0

So I have an assignment to allocate enough memory to store a set of structs with different values in them. I am only allowed to call malloc once.

A have a struct that looks like this:

typedef struct ObjectTag Object;
struct ObjectTag
{
    char *stringA[20];
    char *stringB[30];
    char *stringC[5];
    unsigned short num;
};

For my malloc call I am doing

Object *myObjArray;
myObjArray= (Object*)malloc(numObjects * sizeof(Object));

I am trying to get information from an infile to place into my structs. Thus I am doing

fscanf(inFile, " %s", (*Object)[i].stringA);
fscanf(inFile, " %s", (*Object)[i].stringB);
fscanf(inFile, " %s", (*Object)[i].stringC);
fscanf(inFile, " %hu", (*Object)[i].num);

and so on in a loop.

But when I try to compile I get this error

format '%s' expects argument of type 'char *', but argument 3 has type 'char **'

and also this for the last one

format '%hu' expects argument of type 'short unsigned int *', but argument 3 has type 'int'

I have no idea what I'm doing wrong.

Notes that may be relevant:

  • The infile has an expected format to it
  • I have counted how many structs I needed as noted by the numObjects in the malloc call

1 Answer 1

2

char *stringA[20] is NOT a string of 20 (or 19) characters. It is an array of 20 strings of unspecified length. Are you sure this is what you want?

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

3 Comments

Wow I feel dumb, do you know why the second error is still happening though? The %hu one.
Because to input a number (whether it is int, short, long, float, double etc.) you should give its address to fscanf, like int foo; fscanf("%d", &foo); It is because no function can change the value of its argument.
Doh that makes so much sense. Sorry to bother with the lame question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.