6

I know this is a very basic problem, but I cannot move forward without it and its not clearly explained elsewhere.

Why is this programming giving me so many errors of undeclared identifier? I have declared it, though.

These are the error i am getting.

Error   2   error C2143: syntax error : missing ';' before 'type'
Error   3   error C2065: 'ptr' : undeclared identifier
Error   4   error C2065: 'contactInfo' : undeclared identifier
Error   5   error C2059: syntax error : ')'
Error   15  error C2223: left of '->number' must point to struct/union

and more...

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

typedef struct contactInfo
{
    int number;
    char id;
}ContactInfo;


void main()
{

    char ch;
    printf("Do you want to dynamically etc");
    scanf("%c",&ch);
    fflush(stdin);


        struct contactInfo nom,*ptr;
        ptr=(contactInfo*)malloc(2*sizeof(contactInfo));

    nom.id='c';
    nom.number=12;
    ptr->id=nom.id;
    ptr->number=nom.number;
    printf("Number -> %d\n ID -> %c\n",ptr->number,ptr->id);

}
Veger
  • 37,240
  • 11
  • 105
  • 116
SLearner
  • 1,419
  • 5
  • 18
  • 22
  • Never call `fflush` on `stdin` - it's UB. – Paul R Jan 23 '13 at 09:09
  • maybe you should read the C book with more attention – Aniket Inge Jan 23 '13 at 09:10
  • @PaulR why so? I've read that whenever you need to take two input from unser using scanf you do a fflush to remove the \n in the pipeline? – SLearner Jan 23 '13 at 11:06
  • 1
    @shashlearner: `fflush` is only valid for output or update streams - use `fpurge` for input streams, or just call `getchar()` to discard an unwanted character. – Paul R Jan 23 '13 at 11:14
  • @PaulR ok. But still the code is faulty. I have removed the typecast and used sizeof *ptr, still there are 20 errors! – SLearner Jan 23 '13 at 11:21

5 Answers5

5
typedef struct contactInfo
{
    int number;
    char id;
}ContactInfo;

This code defines 2 things:

  1. a type named ContactInfo
  2. a struct named contactInfo

Notice the difference of the c and C!

In your code you are using a mixed combination of both, which is allowed (although confusing IMHO).

If you use the struct variant you need to explicitly use struct contactInfo. For the other variant (ContactInfo) you must to omit the struct part as it is part of the type definition alteady.

So be careful with both different definitions of your structure. Best would be to only use either one of the variants.


I do not have Visual Studio at hand, but the following (corrected) code compiles with gcc properly without any warnings:

#include<stdlib.h>

typedef struct contactInfo
{
    int number;
    char id;
}ContactInfo;


void main()
{
    ContactInfo nom,*ptr;
    ptr=malloc(2*sizeof(ContactInfo));    
}

(I left out the not so interesting/unmodified parts of the code)

Veger
  • 37,240
  • 11
  • 105
  • 116
3

This:

ptr=(contactInfo*)malloc(2*sizeof(contactInfo));

is wrong, there's no type called contactInfo.

There's a struct contactInfo, which is typedef:d as ContactInfo. C is case-sensitive (and you must include the struct unlike how it works in C++).

Also note that the quoted line is better written as:

ptr = malloc(2 * sizeof *ptr);

Since casting the return value of malloc() is a bad idea in C, I removed the cast. Also, repeating the type is a bad idea since it makes the risk of introducing error greater, so I removed that as well.

Note that sizeof *ptr means "the size of the the type that ptr points at" and is a very handy thing.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • but if i don't typecast malloc, will it not return char type? – SLearner Jan 23 '13 at 09:20
  • Ok so i removed the typecast, but still its showing me the same errors. I am using VS 2010 and have changed the compiler to c\tc i still dont get what's the problem here @unwind . thanks though! – SLearner Jan 23 '13 at 11:03
0

Replace

ptr=(contactInfo*)malloc(2*sizeof(contactInfo));

with

ptr=malloc(2*sizeof(struct contactInfo));
akp
  • 1,753
  • 3
  • 18
  • 26
  • I tried but its still not working. Is the VS 2010 compiler screwing it up? almost all the errors are pointing to the ptr=(contactInfo*)malloc(2*sizeof(contactInfo)); line also removing the typecast adds more error to the program :/ – SLearner Jan 23 '13 at 11:10
  • @akp: see http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Paul R Jan 23 '13 at 12:05
  • OK - if you fix your answer I'll happily remove the down-vote. – Paul R Jan 23 '13 at 14:05
0

C is a case sensitive language.

ptr=(contactInfo)malloc(2*sizeof(contactInfo));

which should be:

ptr=malloc(2*sizeof(ContactInfo));

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • `contactInfo` and `ContactInfo` are 2 different things. So besides case-sensitivity, this is something to keep in mind! – Veger Jan 23 '13 at 09:17
  • I agree, contactInfo is a struct tag, ContactInfo is a typedef of that struct.. if I am right @Veger – Aniket Inge Jan 23 '13 at 09:22
  • That is right, hence my comment that case-sensitivity is not the only problem here. – Veger Jan 23 '13 at 09:25
-1
struct contactInfo nom,*ptr;
ptr=(contactInfo*)malloc(2*sizeof(contactInfo));

here you are using contactInfo to typcast where as it should be struct contactInfo. and as you have typedef it into ContactInfo you can use that also.

    struct contactInfo nom,*ptr;
    ptr=(ContactInfo*)malloc(2*sizeof(ContactInfo));
Tushar Mishra
  • 177
  • 1
  • 7