3

I need to define a type-struct in C that contains an array to be malloc'd as:

#include <stdio.h>
#include <stdlib.h>

typedef struct mine
{
    int N;
    double *A;
} mine;

int main(int argc, char** argv) 
{
    int i;
    mine *m=malloc(sizeof(mine));
    printf("sizeof(mine)=%d\n",sizeof(mine));
    scanf("Enter array size: %d",&(m->N));
    m->A=malloc((m->N)*sizeof(double));
    for(i=0; i < m->N; i++)
        m->A[i]=i+0.23;
    printf("First array element: %lf",m->A[0]);

    return (EXIT_SUCCESS);
}

The program compiles and runs, and the integer assignment seems to work fine. The array is not working as it should, however.

Any suggestions? I would like m to remain a pointer (to pass to functions etc.).

Thanks.

10
  • 2
    What's the problem with the array? (i.e. what output do you get, and what do you want?) Commented May 20, 2013 at 14:32
  • 7
    What does "not working as it should" mean? Commented May 20, 2013 at 14:33
  • 1
    Stylistic and safety sidenotes: 1. malloc(sizeof(mine)); - rather malloc(sizeof(*m));; 2. &(m->N) - try &m->N instead; 3. printf("sizeof(mine)=%d\n",sizeof(mine)); is UB, use the %zu format specifier and look up what's they type of the value sizeof() yields; 4. m->A=malloc((m->N)*sizeof(double)); - some whitespace, readability and safety doesn't hurt, write m->A = malloc(m->N * sizeof(m->A[0])); instead. Commented May 20, 2013 at 14:36
  • 2
    scanf doesn't take a "prompt" string. Always check it's return value before using its outputs! Commented May 20, 2013 at 14:38
  • 3
    @H2CO3: I could argue that sizeof(*m) is safer than sizeof(mine)) in case decide to change the type of m from mine to yours. Commented May 20, 2013 at 14:49

2 Answers 2

8

This is your problem:

scanf("Enter array size: %d",&(m->N));

It should be two separate steps:

printf("Enter array size: ");
scanf("%d",&(m->N));

(and for debugging checking:)

printf("The size entered appears to be %d\n", m->N);

That way, you know if you got the value you intended to get!

Sign up to request clarification or add additional context in comments.

1 Comment

+1 It is worth mentioning to check the result of scanf() to ensure an assignment was made (in this case that an int was read and assigned to m->N).
0

If @abelenky answered your question fine, but I was always told to cast the results of malloc from the void * it returns into whatever you are actually working with.

mine *m = (mine *)malloc(sizeof(mine));

3 Comments

In C, unlike C++, you don't have to cast the resulting pointer from malloc. And I usually don't cast when it is not necessary, as that may hide another problem.
I've always seen mixed feelings on the cast, so I usually let it be. Thanks, though.
No problem. I didn't know the difference between the C and C++ specs, but learned C++ first which is probably why I still cast it. Thanks for pointing that out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.