Thread: basic pointer question need help

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    6

    basic pointer question need help

    I'm trying to work with a dynamically sized "array" of a structure called atoms. I declare an atom pointer like so:

    atom *atoms;

    Then, I allocated memory for a bunch of atom structures like so:

    atoms = (atom *)malloc(100*sizeof(atom));

    Then, I loop from 1 to 100 trying to set values in each atom structure from one to 100 by using commands such as:

    atoms[i].atom_name = atom_name_variable;
    atoms[i].atom_type = atom_type_variable;

    However, when I break out of this for loop, and using Microsofts Visual C++ debugger, I only have one atom object, the first one I processed and not 100 like I should have. What am I doing wrong here?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I loop from 1 to 100

    Loop from 0 to 99.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    Sorry, I do loop from 0 to 99, but I don't know why I don't seem to get a list of 100 atom objects, unless Visual C++'s debugger is not showing me things properly.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I don't see a problem in the code you are posting; I would guess that your problem is in the code that you aren't posting. Is there some minimal bit you could post that would give this its necessary context?
    • What is atom_name? An array of char? A pointer? An int?
    • What is atom_type?
    • Where and how are you changing i?
    • What does the loop look like?
    • Are you checking malloc's return value?
    Here is an example.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct sAtom
    {
        char name[32];
        char type[16];
    };
    
    int main(void)
    {
        int i, count = 100;
        struct sAtom *atom = malloc(count * sizeof(*atom));
        if ( atom != NULL )
        {
            for ( i = 0; i < count; ++i )
            {
                sprintf(atom[i].name, "Name%02d", i);
                sprintf(atom[i].type, "Type%02d", i);
                printf("i = %2d, name = \"%s\", type = \"%s\"\n", i, 
                       atom[i].name, atom[i].type);
            }
            printf("i = %d\n", i);
            free(atom);
        }
        return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    6
    it's the darn Microsoft Visual C++ debugger. It must have a glitch in how it's displaying variable values, because I was able to manually verify that the values are being populated correctly. Sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. function pointer question
    By andrea72 in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 06:25 AM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM