Thread: Accessing structure member through pointer using dynamic memory allocation

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    69

    Accessing structure member through pointer using dynamic memory allocation

    Hello,

    Code:
    #include<stdlib.h>
    struct name {
       int a;
       float b;
       char c[30];
    };
    int main(){
       struct name *ptr;
       int i,n;
       printf("Enter n: ");
       scanf("%d",&n);
       ptr=(struct name*)malloc(n*sizeof(struct name));
    /* Above statement allocates the memory for n structures with pointer ptr pointing to base address */
       for(i=0;i<n;++i){
           printf("Enter string, integer and floating number  respectively:\n");
           scanf("%s%d%f",&(ptr+i)->c,&(ptr+i)->a,&(ptr+i)->b);
       }
       printf("Displaying Infromation:\n");
       for(i=0;i<n;++i)
           printf("%s\t%d\t%.2f\n",(ptr+i)->c,(ptr+i)->a,(ptr+i)->b);
       return 0;
    }
    In the above code i would like to use (.) Operator in scanf and printf statements but i am getting some errors, please help me how can i use (.) operator in scanf and printf statement.


    scanf("%s %d %f", &(*ptr+i).c, &(*ptr+i).a, &(*ptr+i).b);


    printf ("%s %d %f\n", (*ptr+i).c, (*ptr+i).a, (*ptr+i).b);

    Error:

    [Error] invalid operands to binary + (have 'struct name' and 'int')
    Last edited by Shankar k; 09-21-2015 at 07:26 PM.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Use (*(ptr+i)). ... . You need to add the index to the pointer before dereferencing it. You may not need the outer parenthesis, I didn't try to compile the code.
    Last edited by rcgldr; 09-21-2015 at 08:21 PM.

  3. #3
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    You should see struct name *ptr as a variable that holds the address of another name struct. *ptr holds no actual data about the contains of a name struct itself only the location where the data should be stored. malloc was given the task to find a nice place somewhere where all that data could be physically stored and all *ptr has to do is remember the location of this data block.

    Because *ptr itself has no members like a, b or c, *ptr only holds the location where they are stored, you cannot say ptr.a, ptr.b or ptr.c because they simply don't exist.

    (ptr + i)->c could be read as: point to start location of data block + size of struct name * i + a + b.
    This parameter is reserved

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Or you could simplify the notation by using

    Code:
           scanf("%s%d%f",ptr[i].c,&ptr[i].a,&ptr[i].b);
    ...
           printf("%s\t%d\t%.2f\n",ptr[i].c,ptr[i].a,ptr[i].b);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic memory allocation using pointer to pointers
    By mp252 in forum C++ Programming
    Replies: 12
    Last Post: 06-22-2013, 05:34 PM
  2. structure dynamic memory allocation?
    By mgracecar in forum C Programming
    Replies: 4
    Last Post: 03-15-2012, 11:15 PM
  3. Accessing Member from Structure Array
    By rrc55 in forum C Programming
    Replies: 4
    Last Post: 10-18-2009, 06:20 AM
  4. Regarding accessing the structure member
    By kollurisrinu in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 04:51 AM
  5. Dynamic Memory Allocation
    By schifers in forum C Programming
    Replies: 12
    Last Post: 05-14-2008, 01:49 PM