Problem in Reading values from structure

This is a discussion on Problem in Reading values from structure within the C Programming forums, part of the General Programming Boards category; Hi all I have written the code for writing values to structure and then reading from it but I am ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    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.

    Ex.
    .
    ABC 12 XYZ 14
    The code is as follow:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    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));
         memcpy(p,ptr,size_struct);  
         t = (struct test *)malloc( sizeof(len));
         
         printf("\n Enter Name:");
         gets(name);
         printf("\n Enter Age:");
         scanf("%d",&i);
         t->age = i;
         strcpy(t->name,name);
         memcpy(p+size_struct,t,len);
         *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++)
      {
        writeToStruct(&t);
      }
    
      readFromStruct(&t);
     
    
    }

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

    Thanks

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649

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

    but need help in second one.

    Thanks

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    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.
    Cprogramming.com 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01: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, 01:46 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21