Thread: Array of structs

  1. #1
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70

    Array of structs

    is this possible ??

    Code:
    struct s{
    char *name;
    }MyArray[5];
    is this will create array of structs or i am wrong because after compiling it i get the same answer ..

    exampl when i put to the first element "first_name"
    and the second element "second_name"
    and if i output the first elemenet .. it will contain "second_name"..
    to be a 1-1, learn C

  2. #2
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    this is More advance exmaple :

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define MAX2 100
    
    struct s 
    {
    	char* id;              //array of structures Maximum of Five students,
    };
    
    int main(int argc,char *argv[])
    {
    
    	struct s MyArray[5];
    	FILE *fp;
    
    	char *r = NULL;
    	char d[2];
    
    	int n,j,k,m,l;
    	int i=0;// counters
    	int u=0;
                     
    	
    	char line[MAX2];
    
    
    	if ((fp = fopen(argv[1],"r")) == NULL){
    	    printf("cannot open the file that you specified\nThe file has to be in the same directory\n");
    	    exit(1);}   
                    d[0] = ':';
                    d[1] = '\0';              
    	
    	i = 0;
    	while((fgets(line,100,fp)) != NULL  )
    	{
    		n=0;
    		for(k = 0; k < 100; k++){
    			if (line[k] == '\n'){
    				line[k] = '\0';
    				break;
    			}
    
    		}
    
    		r = strtok(line,d);
    		MyArray[i].id = r;
    		n = strlen(r);
    		MyArray[i].id[n] = '\0';
    
    		++i;
    		
    	}
    
    	printf("%s\n",MyArray[0].id);
    	
    	fclose(fp);
    	return 0;
    }

    the file-input

    Code:
    s1:csc212,csc281,csc361
    s2:csc361,csc336,csc329
    s3:csc336,csc327,csc212,csc281
    s4:csc342,csc327,csc311,csc305
    s5:csc212,csc281,csc327,csc380
    output of the program:

    Code:
    s5
    why every element in the array print s5 , i think it should print s1 ???
    to be a 1-1, learn C

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you have a pointer, so everything ends up pointing at the same thing.

    So have something like
    char id[20];

    And
    strcpy( MyArray[i].id, r );


    > n = strlen(r);
    > MyArray[i].id[n] = '\0';
    These two lines are a waste of time. strtok() writes a \0, and strlen() returns the index position of the \0 (otherwise known as the length).
    So all you're doing is overwriting one \0 with another \0.


    > for(k = 0; k < 100; k++)
    See the FAQ, there are easier ways of removing the \n.
    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.

  4. #4
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    thanks Salem , and is there a way to do it dynamicly ??
    to be a 1-1, learn C

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Code:
    struct s* MyArray = malloc(num*sizeof(struct s))
    Will give you num stucts.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    ummm...no. id is a char*, so if you wanted to do that, you'd want to allocate chars instead of struct s's.
    Last edited by Happy_Reaper; 05-05-2007 at 04:14 PM. Reason: EDIT : Meshal, why'd you delete your post ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  7. #7
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    and if i have a struct that contain multiple id`s .. like this :

    Code:
    struct s{
    char *id[4] ;
    }MyArray[5];
    can i do what salem say`s .. or it will be a problem

    i mean is this true :

    Code:
    MyArray[i].d[j] = malloc(sizeof(char*));
    strcpy(MyArray[i].id[j],r);

    so what is the solution to this problem !!
    Last edited by Meshal; 05-05-2007 at 06:16 PM.
    to be a 1-1, learn C

  8. #8
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    thanks guys , finally i got every things works Ok .. "thanks word" is not enough to thank you..

    and i appreciate your help all of you .

    sorry my english is bad ,
    Last edited by Meshal; 05-05-2007 at 07:53 PM.
    to be a 1-1, learn C

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > MyArray[i].d[j] = malloc(sizeof(char*));
    > strcpy(MyArray[i].id[j],r);
    It should be
    Code:
    if ( (MyArray[i].d[j] = malloc( strlen(r) + 1 )) != NULL ) {
        strcpy(MyArray[i].id[j],r);
    } else {
        // out of memory
    }
    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: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  4. array of structs initialization - PLZ help...
    By Vanya in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2002, 08:10 PM
  5. Pointer to Array of Structs
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 08:34 AM