I just have a quick question.

Say I have a struct for the purpose of commenting in which it could be filled up or left blank depending on the user. Therefore, I need to dynamically allocate memory to the array comments.

typedef struct userComments {
     char comments;
} name;

So, what I’d do is to define 2 pointers: one to allow for pointing to the current record in the list, and the other to dynamically allocate memory to the char comments.

name *currentC, *comments;

In the main() section of the code, I would perform a malloc()

comments = (name *)malloc(sizeof(name));

However, subsequently, if I wanna get user comments, say from the below function:

void modifyComments() {
     printf(“Update Comment: “);
     gets(currentC->comments);
}

Visual Studio 2010 prompts an error message at this point, stating that:
Name *currentC;
Error: argument of type “char” is incompatible with parameter of type “char *”.

Had I assigned it statically, i.e.

typedef struct userComments {
     char comments[40];
} name;

I would not encounter this problem.

I’m curious as to where I have gone wrong in my “accessing/updating” the array within the struct dynamically.

My lecturer told me this:

One way to overcome this is to give gets() access to a temporary char[], and then strcpy the result into the memory allocated and tracked by the pointer <comments>.

So, what I did was:

void modifyComments() {
     char commentsTmp[40];

     printf(“Update Comment: “);
     strcpy(comments, gets(commentsTmp));
}

Now for the comments part (highlighted in red),
Visual Studio 2010 prompts the error:
Error: argument of type “name *” is incompatible with parameter of type “char *”

Did I do this correctly, or am I still making a mistake somewhere?

Recommended Answers

All 3 Replies

Your problem is that you do not have a clear concept on type.
first error:
Error: argument of type “char” is incompatible with parameter of type “char *.
gets() only accepts char* variable, so complied error pops up if you pass a char variable to it.
second error:
even you have only one char* inside your struct, name* is not equal to char* !!!!!
you should replace the red word with comments->commnets instead of comments.

Your original code is almost right. Just take a look at your structure definition. You want comments to hold a string, but you have defined it as a single character. That's why you're getting an error with gets.

Now, gets won't allocate the variable for you so you'll need to use malloc for comments too. Or you could define it statically like in your second example.

If the user inputs more data than comments can hold, you'll get undefined (and probably unwanted) behavior, so use fgets(currentC->comments, size, stdin); where size is the number of bytes you've allocated for comments .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.