Thread: looping struct

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    16

    looping struct

    Hello,

    Thanks in advance. I like to loop through the members of the following struct. How can I do that?

    Code:
    /* 
      I will store the adress of the struct in n, so that it ppoints back the the start of the struct
    */
    
    struct number {
      int number1;
      int number2;
      int number3;
    
      struct n *loopback;
    };

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The struct is in memory. Everything that is in the memory can be pointed out. Thus, if you had a pointer in number1, then you could increment the pointer the size_of an int (system depentent <- spelling, but you get my point), thus having the pointer going through your data members. This is just an idea I have.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    ahh, yes. I forgot I have to use pointers:
    Code:
      struct number *p;
    
      p = &number;
      
      int i;
      for(i=0;i<5;i++) {
         printf("number in buffer is %i\n",*p );
    I read about creating a pointer to an array, then increment that pointer by x, to get the next array element, but did not know how it was done with structs. Now I know.

    almost there.... now get the 2nd element, etc
    Last edited by chihwahli; 03-21-2013 at 03:31 PM.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Are you sure you got the desired results with this code?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    16

    Thumbs up

    Not yet, my C books says in the arrays area: the pointer variable plus one is the 2nd element in an array.
    So I try
    Code:
       printf("number in buffer is %i\n",*p + i );
       or
       printf("number in buffer is %i\n",(*p) + i) ; 
       or
         printf("number in buffer is %i\n",*(p+i) );
    The last line "works", but it points to some strange memory location, the results are not what they should be.
    The results printed are:

    1000
    4214816
    0
    1
    0

    Hmmm, could you give me another pointer?

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    22
    @chihwahli

    I'm a little confused on your end goal here but I'll give it my best shot to answer what your asking.

    When you read memory all your doing is grabbing the appropriate number of bytes, the name of a struct (like an array) points to the first element of the struct so:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct number {
      int number1;
      int number2;
      int number3;
    };
    int main(void)
    {
        // Create an instance of the struct
        struct number numStruct;
        
        // Initialize struct variables
        numStruct.number1 = 0;
        numStruct.number2 = 1;
        numStruct.number3 = 2;
        
        // Get a pointer to the first integer of the struct
        int* numPtr = (int*)&numStruct;
    
        // Print the struct by retreiving each int
        int i;
        for(i = 0; i <= 2; i++)
        {
            // Bump through to the next integer
            printf("%d\n",*(numPtr + i));
        }
        return 0;
    }
    This will print out each number in the struct using the for loop.

    I'm unsure what the purpose of the loopback in your struct is, if you can give a little more detail I'll dive into it.

    I threw this together in about a minute so don't critique it too hard

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I think you are searching for this
    Code:
    #include <stdio.h>
    
    struct foo{
        int a1;
        int a2;
        int a3;
        int a4;
        int a5;
        int a6;
        int a7;
        int a8;
        int a9;
        int a10;
        
        
    };
    
    int main(void)
    {
        struct foo test;
        test.a1 = 1;
        test.a2 = 2;
        test.a3 = 3;
        test.a4 = 4;
        test.a5 = 5;
        test.a6 = 6;
        test.a7 = 7;
        test.a8 = 8;
        test.a9 = 9;
        test.a10 = 10;
    
        int *p;
     
        p = (int*)&test;
     
        int i;
        for(i=0;i<10;i=i+1) {
            printf("a%d is %d\n",i+1,*(p+i));
        }
        
        return 0;
    }
    But if you have only arrays, you should use an array
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    22
    Quote Originally Posted by std10093 View Post
    I think you are searching for this
    But if you have only arrays, you should use an array
    Beat ya to it

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Tomwa View Post
    Beat ya to it
    haha true... But what if you had a long, an int and a string? Could you loop through them? Maybe not... Use an array for this kind of operations!!!

    Tip: In C++ things can become more tricky and in java you could use reflection
    (I think garbage collectors use that (in some way)..)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Thanks Tomwa and std10093. You both gave the same answer.
    &Tomwa: It's just one small pice of the whole program I need to write. I like to try small sample programs to understand things bit by bit. The end program is a consumer/producer program with threads. Wanted to use structs.


    I see now , what I did wrong. Because I want to print the value, I have to create a pointer to int and not a pointer to the struct. If I casted the struct type to int pointer type, that would have worked as well, but one line of extra code. Is that correcty formulated?

    Edit: you guys seem to know eachother? =)

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I do not know Tomwa

    Don't ask if it will run, test it
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Both code sniplets works perfectly! Thumbs up!

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Good. I do not see where this code can be very useful, but it was fun writing it...so I upload it here. If you have any suggestions guys, tell them ;p
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Its a way to write an looping buffer. Going to use it in a program with 2 threads that share this buffer. One creates numbers and the other gets them off the buffer.

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Why don't you use an array instead of misusing a struct?

    The data structure you want to use is called a queue.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05:38 PM
  3. Looping struct info
    By kokoro in forum C Programming
    Replies: 4
    Last Post: 11-26-2009, 10:36 AM
  4. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  5. Replies: 1
    Last Post: 05-05-2004, 06:58 AM

Tags for this Thread