accesing pointer structures

This is a discussion on accesing pointer structures within the C Programming forums, part of the General Programming Boards category; i am trying to search for a string in a structure allocated for 100 clients, just get confused on how ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    17

    accesing pointer structures

    i am trying to search for a string in a structure allocated for 100 clients, just get confused on how to access variables in ptr structure for example
    Code:
    struct fac
    { 
    int a[50];
    int b[50];
    int c;
    struct res * r;
    };
    
    struct res 
    {
    int a1;
    int b2;
    };
    
    int main ()
    {
    struct fac data[100];
    data[100]= (struct fac *) malloc ( 4 * sizeof(struct fac))
    }
    i think i allocated memory for 100 clients, now lets say i wanted to search for a string in struct fac int a , how do you locate the whole record in the structure and print it???
    any help would be appreciated for some reason i have no clue how to do this

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    You actually only allocated memory for the 101st element in the array, which actually doesn't exist, atleast its not part of the array. If you declare an array of structs, you dont need to allocate memory for them, the declaration holds memory for them. If you need to assign them something, just do it like this:
    Code:
    int main()
    	int i;
    	struct fac data[100];
    
    	for (i = 0; i < 100; i++)
    		data[i] = someting;
    	return 0;
    }
    If you wanted to search through them looking at a, just do something like this:
    Code:
    int main()
    	int i;
    	struct fac data[100];
    
    	for (i = 0; i < 100; i++)
    		data[1] = someting;
    
    	//search through the array for string a
    	for (i = 0; i < 100; i++)
    		if (data[i].a == something)
    			//do something here for true
    
    	return 0;
    }
    Last edited by stumon; 04-02-2003 at 06:28 PM.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    17
    ok, when looking for a string in a certain part of the structure and you find it, how do i get all info in struct where the string was located ??

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    if its in an array like yours is. Just use the name of the array and the element number. Say the 34th member in the array has the correct string. you would just use the array name and element number like this
    Code:
    *something = data[33];
    
    //or if you are still in the for loop and  you have an if determains is this is the right string, just say
    *something = data[i];
    If you wanted those members also seperate, just say
    Code:
    char blah = something->a;
    char blah2 = something->b;
    int blah3 = something->c;
    ***By the way, you declared all the members in the struct to int, so if you meant (by your first question) for those to be char arrays, or strings, change them in the struct to char data type.
    Last edited by stumon; 04-02-2003 at 07:05 PM.
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  2. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 08:40 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM

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