How to define a pointer to a structure

28,715

Solution 1

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)

Solution 2

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.

Share:
28,715
SLearner
Author by

SLearner

I am still learning.

Updated on August 17, 2022

Comments

  • SLearner
    SLearner almost 2 years

    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
    Veger over 11 years
    contactInfo and ContactInfo are 2 different things. So besides case-sensitivity, this is something to keep in mind!
  • SLearner
    SLearner over 11 years
    but if i don't typecast malloc, will it not return char type?
  • Aniket Inge
    Aniket Inge over 11 years
    I agree, contactInfo is a struct tag, ContactInfo is a typedef of that struct.. if I am right @Veger
  • Veger
    Veger over 11 years
    That is right, hence my comment that case-sensitivity is not the only problem here.
  • SLearner
    SLearner over 11 years
    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
    SLearner over 11 years
    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
    SLearner over 11 years
    i tried and its not working. I think there are other problems as well?
  • Veger
    Veger over 11 years
    I added a fixed version of your code that compiles properly with gcc to my answer to show the required corrections
  • Paul R
    Paul R over 11 years
  • Paul R
    Paul R over 11 years
    OK - if you fix your answer I'll happily remove the down-vote.