Thread: Lost in structures and pointers

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    11

    Lost in structures and pointers

    The situation is that I am going to have a couple of typedef struct's, something like (of course more complicated

    Code:
    typdef struct {
      double     *arrPtr1;
      double     *arrPtr2;
    } arrays;
    
    typedef struct {
        ...
        ...
        ...
      arrays      *array;
        ...
        ...
    } structure;
    So there's structure which includes a pointer to an arrays struct, which has pointers inside of it.

    I'm going to have to dynamically supply memory to the arrays through malloc, and then funnel a calculation into these arrays.

    I'm unsure of the mechanics accessing these arrays for the malloc and subsequent use, and I haven't worked much with structs so hoping for some advice. Just trying to lay it out for myself at the moment.

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,243
    I suggest that you approach this fby experimenting in steps:

    1. Given this struct definition:
    Code:
    typedef struct foo
    {
        int x;
    } foo;
    Write a program that defines an object of foo and uses its x member.

    2. Given this pointer:
    Code:
    int *x;
    Write a program that uses malloc to allocate memory for an array of ints, and store the resulting pointer value in x. Make use of the dynamic array through x, then call free when you are done.

    3. Combine what you understand from steps 1 and 2 to do what you originally set out to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    The first two I can do all right, the following came out clean in Valgrind and produced expected results. I suppose I'm lost on the logic of what I'm trying to do in the end.

    Code:
    int main()
    {
    typedef struct foo
    {
        int x;
    } foo;
    
    int *x, *xstart;
    
    foo integer;
    
    integer.x = 5;
    
    printf("should get 25:  %d\n", 5*integer.x);
    
    x = (int *) calloc(10, sizeof(int));
    xstart = x;
    
    
    for(x = xstart; x < xstart + 10; ++x)
    {
        *x += 2;
        printf("%d\n", *x);
    }
    
    
    free(xstart);
    return 0;
    }
    So I naively tried something like:
    Code:
    int main()
    {
      typedef struct foo
     {
          int *x;
     } foo;
    
      foo array;
      int *start;
    
      array.x = (int *) calloc(10, sizeof(int));
      start = array.x;
    
      for(array.x = start; array.x < start + 10; ++(array.x))
      {
          array->x = 1;
          printf("%d\n", array->x);
      }
    
      free(start);
      return 0;
    }
    and wound up with a disaster.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,646
    In the "disaster" case, I think only the code near for loop needs to be fixed:

    Code:
        for(start = array.x ; start < array.x + 10; ++start)
        {
            *start = 1;
            printf("%d\n", *start);
        }
    
        free(array.x);

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    That works quite well! I was maybe a bit closer than I thought.

    Thanks!

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,646
    Quote Originally Posted by shallowbay View Post
    That works quite well! I was maybe a bit closer than I thought.
    Closer than I thought also. All you really needed to change was two lines in the for loop:

    Code:
            *(array.x) = 1;
            printf("%d\n", *(array.x));
    I used a more "conventional" approach by using the local variable start to iterate through the data in array.x. What your program did was copy the pointer from array.x into start, and with the fix shown in this post, used array.x to iterate through the array, then freed the memory using the copy of the pointer stored in start, but this is less "conventional".
    Last edited by rcgldr; 05-30-2013 at 11:36 PM.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    I like they way you did it better as it's certainly more natural.


    Code:
    int main()
    {
      typedef struct foo
     {
          int *x;
     } foo;
     
      foo array;
      int *start;
     
      array.x = (int *) calloc(10, sizeof(int));
      start = array.x;
     
      for(array.x = start; array.x < start + 10; ++(array.x))
      {
          *(array->x) = 1;
          printf("%d\n", *(array->x));
      }
     
      free(start);
      return 0;
    }
    I had actually tried this first, but I get an error: invalid type argument of '->' (have 'foo') at which point I tried switching to just using array->x.

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,646
    Quote Originally Posted by shallowbay View Post
    ... *(array->x) ...
    I corrected my previous post, that should have been *(array.x):

    Code:
            *(array.x) = 1;
            printf("%d\n", *(array.x));
    but as mentioned before, you should leave array.x alone and use start to iterate through the array of number, and since you're using "start" to iterate through array.x, you might want to choose a better name, perhaps something like "intptr" (integer pointer), or "ptr2int".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hel plz im lost on pointers
    By padizzel in forum C Programming
    Replies: 3
    Last Post: 02-28-2011, 12:05 AM
  2. A little lost and confused with my pointers :/
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 6
    Last Post: 05-01-2009, 01:32 PM
  3. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  4. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM