Array of pointers to structs

This is a discussion on Array of pointers to structs within the C Programming forums, part of the General Programming Boards category; I'm trying to write a queue for this project. Suppose to use a struct, got that part. We had to ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    8

    Array of pointers to structs

    I'm trying to write a queue for this project. Suppose to use a struct, got that part. We had to write this before, but just using ints not structs. I'm having problems adding something to the queue. Here's the basic layout:

    I have a struct which can be referenced by using AddrT, or AddrTP which returns a pointer to a struct.

    I have a an AddrT *x in main for an array of structs, this will be the queue.

    Once the program gets run the user has the option of selecting "a" and typing in the customer ID, first, last name. At this part I have a an AddrT called toAdd, and an AddrTP called add here is the code for it


    Code:
    				scanf("%d%s%s", &id,&first,&last);
    				toAdd.custID = id;
    				strcpy(toAdd.firstName, first);
    				strcpy(toAdd.lastName, last);
    				add = &toAdd;
    				printf("\n");
    				if(addCust(add) == 0)
    					printf("Not added\n");
    addCust takes in an AddrTP called cust, and returns 1 if successful 0 if not, code for this method is:

    Edit: I found the error, it should have been == NULL, derrrrr

    Code:
    if((x = realloc(x, (count + 1) * sizeof(AddrTP))) == NULL){
    		return 0;
    	}
    	x[count - 1] = cust[0];
    	count++;
    	return 1;
    The problem is, whenever I run the program and select a and type in the information in, it always returns back a 0 and tells me it wasn't added. Any suggestions?
    Last edited by lpmrhahn; 04-17-2004 at 09:45 AM. Reason: Found it

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Any suggestions?
    Perhaps you should return 0 when realloc fails rather than when it succeeds. And it's not a good idea to assign the return of realloc to the same pointer as its first parameter. If it fails and returns NULL, you've lost everything. This is better:
    Code:
    /* T is a generic type, replace it with your structure type */
    T *save = realloc ( x, new_size );
    if ( save == NULL ) {
      return 0;
    }
    x = save;
    ...
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  3. Creating an array of pointers to int[]
    By OkashiiKen in forum C Programming
    Replies: 3
    Last Post: 09-29-2006, 06:48 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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