problem in structs

This is a discussion on problem in structs within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> #include<string.h> struct list { char first[20]; char name[20]; char blahblah[40]; }; void get_struct(struct list * x); main() { ...

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    problem in structs

    Code:
    #include<stdio.h>
    
    #include<string.h>
    
    struct list
    
    {
    
           char first[20];
    
           char name[20];
    
           char blahblah[40];
    
           };
    
        void get_struct(struct list * x);
           main()
    
           {
    
           struct list data[4];
    
           struct list *ptr;
    
           ptr=data;
    
           get_struct(ptr);
    
           }
         void  get_struct(struct list *x)
    
           {
    
    
           int i;
    
    
           for(i=0;i<4;i++)
    
           {
        
    
       ((x+i)->first)="storm";  
          
    
           printf("\nfisrt=%s",(x+i)->first);
    
           printf("\nname=%s",(x+i)->name);
    
           printf("\nlif is =%s",(x+i)->blahblah);
    
           strcat((x+i)->first,(x+i)->name);
    
           printf("\nme =%s",(x+i)->first);
    
    
    
           }
    
    
           printf("Good Night");
         
      }
    compiler give me this in line
    Code:
    ((x+i)->first)="storm";
    22 C:\Documents and Settings\St0rM\Desktop\test.c incompatible types in assignment
    why so ?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    can't do that, use strcpy()

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    also
    1. int main should be used
    2. fix indentation
    3. pass array size together with array pointer - don't use "magic numbers" like 4 in the get_struct function
    4. x[i].first is a lot easier to read than ((x+i)->first)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    works prefect
    Code:
    #include<stdio.h>
    
    #include<string.h>
    
    struct list
    
    {
    
           char first[20];
    
    
           char name[20];
    
           char blahblah[40];
    
    
    
           };
    
    
    
        void get_struct(struct list * x);
    
           main()
    
           {
    
           struct list data[4];
    
           struct list *ptr;
    
           ptr=data;
    
           get_struct(ptr);
    
           }
    
         void  get_struct(struct list *x)
    
           {
    
           int i;
    
           for(i=0;i<4;i++)
    
           {
    
        
    
           strcpy((x+i)->first,"storm");  
    
           strcpy((x+i)->name," man");  
    
           strcpy((x+i)->blahblah,"Is nothing But A good Lie");  
    
           printf("\nfisrt=&#37;s",(x+i)->first);
    
           printf("\nname=%s",(x+i)->name);
    
           printf("\nlif is %s",(x+i)->blahblah);
    
           strcat((x+i)->first,(x+i)->name);
    
           printf("\nme =%s\n",(x+i)->first);
    
           }
    
           printf("Good Night");
    
           }
    but why i can't do
    Code:
    ((x+i)->first)="storm";
    ?
    isn't x+i-> access the first array ?
    thanks again

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you can access the array with (x+i)-> operator (although I think x[i]. looks nices)

    but you cannot assigh to array in C...

    To put contents in the char-array - you should use string-manipulation functions like strcpy
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    1. int main should be used
    2. fix indentation
    3. pass array size together with array pointer - don't use "magic numbers" like 4 in the get_struct function
    4. x[i].first is a lot easier to read than ((x+i)->first)

    ahhaaaaaaa
    thanks for this notices 4. x[i].first is a lot easier to read than ((x+i)->first)
    specially this 4. x[i].first is a lot easier to read than ((x+i)->first)
    but you cannot assigh to array in C...
    so i can only access it's content's but i can't assign any thing for it except with string manipulations functions
    thanks for the tips

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When a char array is first declared, you can THEN add a string of char's:

    char_array[] = "It's a pirate's life for me"

    After the initial declaration of the char array, you can alter it char by char:

    char_array[i] = 'P'

    if you like.

    You can use all kinds of equivalent pointer notation with arrays, but they quickly become a PITA to debug, etc. imo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Problem with structs and functions
    By Moose112 in forum C++ Programming
    Replies: 11
    Last Post: 12-01-2006, 10:04 PM
  3. Problem with array of structs
    By Wiretron in forum C Programming
    Replies: 3
    Last Post: 09-03-2006, 02:07 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. register structs - problem declaring
    By Kagey in forum C Programming
    Replies: 11
    Last Post: 11-11-2002, 02:58 AM

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