hey guys, so i get the "c incompatible types in assignment" error in the malloc line.
would someone please explain where i went wrong?

Thank you in advance.

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

struct the_struct
{
 char FirstName[20];
 char LastName[32];
 int  Score[20];
};
int main ()
 {
int i,n;
struct the_struct *ptr[100];
printf("how many students?");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
for (i=0;i<=n;i++);
    printf("Enter First Name \n");
    scanf("%s",ptr[i]->FirstName);
    printf("Enter Last Name \n");
    scanf("%s",ptr[i]->LastName);
    printf("Enter Score? \n");
    scanf("%s",ptr[i]->Score);
    printf("%s %s %s\n",ptr[i]->FirstName,ptr[i]->LastName,ptr[i]->Score);
 }

Recommended Answers

All 10 Replies

First of all ptr is an array (of pointers), not a pointer. So you can't assign to it.

Second of all, even if it were a pointer, it wouldn't be an int pointer, so casting the result of malloc to int* is wrong because that's the wrong type (also: don't cast the result of malloc). Similarly it does not make sense to use sizeof(int) as the basis for your size calculation as, again, it's not an int pointer.

Yes I know its an array of pointers, but I didn't know how to assign like 100 user inputs to diffrent pointers.
So what type should it be so it could hold char and int? Or should I do two malloc statements one to store int and one to store char?

As sepp2k alreay said, you can malloc ptr because it's already an array of 100 pinters. Instead you will have to malloc each individual pointer

for (i=0;i<=n;i++)
{
   ptr[i] = malloc(sizeof(the_struct));
   ...
}

what type should it be so it could hold char and int?

It holds the_struct which is neither char nor int. the_struct is a user-defined data type.

You are making the wrong typecast

One doesn't need to (and arguably shouldn't) typecast the result of malloc at all. And much more importantly, ptr is still not a pointer, so that line won't work no matter what he casts the result of malloc to.

Yes, I realized that afer re-reading your original post. Re-read my post and you will see that I corrected it. And I agree with you about typecasting malloc in C programs.

So this is what i change but i still get errors.
1)the_struct is undeclared
2)each undeclared identifier is reported only once
3)for each function it appers in.

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

struct the_struct
{
 char FirstName[20];
 char LastName[32];
 int  Score[20];
};
int main ()
 {
int i,n;
struct the_struct *ptr[100];
printf("how many students?");
scanf("%d",&n);
for (i=0;i<=n;i++);
{
    ptr[i] = malloc(sizeof(the_struct));
    printf("Enter First Name \n");
    scanf("%s",ptr[i]->FirstName);
    printf("Enter Last Name \n");
    scanf("%s",ptr[i]->LastName);
    printf("Enter Score? \n");
    scanf("%s",ptr[i]->Score);
    printf("%s %s %s\n",ptr[i]->FirstName,ptr[i]->LastName,ptr[i]->Score);
 }
 }

ptr[i] = malloc(sizeof(the_struct));

should be this:
ptr[i] = malloc(sizeof(struct the_struct));

Occasionally I confuse C and C++. In C++ the keyword struct is not required. Sorry for the confusion.

okay that worked, but it works one time only.

and shouldn't i write;

ptr[i] = malloc(sizeof(n*(struct the_struct)));

so it could store number of elements the user input?

So finally made it work thanks to you guys!
and for future reference this is the final code that worked.

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

struct the_struct
{
 char FirstName[20];
 char LastName[32];
 int  Score[20];
};
int main ()
 {
int i,n;
struct the_struct *ptr[100];
printf("how many students?\n");
scanf("%d",&n);

for (i = 0; i < n; ++i)
{

   ptr[i] = malloc(sizeof(struct the_struct));
   printf("Enter First Name \n");
   scanf("%s",ptr[i]->FirstName);
   printf("Enter Last Name \n");
   scanf("%s",ptr[i]->LastName);
   printf("Enter Score? \n");
   scanf("%s",ptr[i]->Score);
   printf("%s %s %s\n",ptr[i]->FirstName,ptr[i]->LastName,ptr[i]->Score);
    }
 }
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.