Thread: Problem in Reading values from structure

  1. #1
    Registered User
    Join Date
    Sep 2010

    Problem in Reading values from structure

    Hi all

    I have written the code for writing values to structure and then reading from it but I am facing some problem in that.

    1) When I try to read the values from stdin, it read first value correctly but for second loop it directly ask for "age" input skipping "name".

    First Loop:

    Enter Name:ABC

    Enter Age:12

    //Second Loop:

    Enter Name:
    Enter Age: / / Directly come here. Don't know why
    2) Secondly, I am writing the code to add values in a structure by increasing the size of structure
    For example,

    In first loop, value stored from 1 to 100 and in second loop value should store from 101 to 200.
    and at the end I assign the base address of structure to a pointer.

    ABC 12 XYZ 14
    The code is as follow:

    static int count = 0;
    struct test
       int age;
       char name[1000];
    void writeToStruct(struct test **ptr)
       int i,size_struct=0,len = 0;
       struct test *p = NULL;
       struct test *q = NULL;
       struct test *t = NULL;
       char name[1000];
       if ( count != 2 )
         size_struct = sizeof(test)*(++count);
         len = sizeof(test);
         p = (struct test *)malloc( sizeof(size_struct));
         t = (struct test *)malloc( sizeof(len));
         printf("\n Enter Name:");
         printf("\n Enter Age:");
         t->age = i;
         *ptr = p;
    void readFromStruct(struct test **ptr)
       int i =0;
       for ( i =0;i<2; i++)
        printf("\n %s %d\n",(*ptr)->name,(*ptr)->age);
    int main()
      test *t=NULL;
      int i = 0;
      for ( i =0;i<2; i++)

    I am getting segmentation fault.
    Any help is will be greatly appreciated.


  2. #2

  3. #3
    Registered User
    Join Date
    Sep 2010
    Solved problem 1.

    but need help in second one.


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    You have far too many (and plain wrong) sizeof expressions.

    It is simply
    p = malloc( sizeof(*p) );

    If you're adding ANY extra characters to that line, then you're doing it wrong.

    In particular, attempting to cast the result of malloc. FAQ > Casting malloc
    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. Replies: 6
    Last Post: 08-15-2010, 07:12 AM
  2. Problem with values changing
    By patra_user in forum C++ Programming
    Replies: 19
    Last Post: 12-12-2007, 02:12 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM