Solved

Newbie question about  structs

Posted on 1998-12-27
13
233 Views
Last Modified: 2010-04-15

// this struct contains pointers (a,b,c,d,e,f,g)  to other structs
// an array of structs?

typedef struct
{
int a
int b
int c
int d
int e
int f
int g
} s;
typedef s *s;

int x;
int y=6;
int *ptr;

// How do I  for next loop associate a ptr to each member of the array ??

for(x=0; x<y;x++)
{
ptr = each succesive member of *s ?????????
// do something with the pointed to structs
}
????????????

Thanks  
0
Comment
Question by:theorange
13 Comments
 

Author Comment

by:theorange
ID: 1255442
should I rephrase this, how do I get each individual pointer ( the array members) successively ???????
0
 
LVL 2

Expert Comment

by:trillo
ID: 1255443
Wait, wait wait..... There are a lot of errors in your code and in your comments:

1)
>>// this struct contains pointers (a,b,c,d,e,f,g)  to other structs
I don't see any pointers in your struct, I only see variables, but not pointers

2)
>>// How do I  for next loop associate a ptr to each member of the array ??
I don't see any array. (A struct is NOT the same as an array)

3)
I don't see any declaration of a "s" objcet which can be used.
----------------------------------
If I understand well, you want to make a variable (int), to take the value of each MEMBER of the STRUCTURE. right?

You could use:
typedef struct
{
    int num[7]
} s;
typedef s *s;
int *ptr; //Integer that will take different values
s *any;   //object you'll use

for(int x=0; x<7; x++)
{
    ptr = &any->num[x];
    // do something with the pointed to structs
}

-------------------------
Trillo
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255444
int *temp;
temp = (int*)s;
for(x=0; x<y;x++)
{
  ptr = temp[x];
  // do something with the pointed to structs
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:rbr
ID: 1255445
first use
typedef struct
{
int a;
int b;
int c;
int d;
int e;
int f;
int g;
} s;
typedef s *s;

int x;
int y=6;
int *ptr;

You cant use a direct way to access to pointer in a loop
ptr= &(s->a); will be a pointer to a
ptr=&(s->b); will be a pointer to b and so on.

If you want to use a loop why don`t you use an array in your struct

typedef struct {
int a[6];
} s;
typedef s *s;

int x;
int y=6;
int *ptr;

// How do I  for next loop associate a ptr to each member of the array ??

for(x=0; xa[x]);
}

Thanks  
0
 
LVL 10

Expert Comment

by:rbr
ID: 1255446
Sorry mistake
the lines should be
for(x=0; x<y;x++)
{
ptr = &(s->a[x]);
}

0
 
LVL 84

Expert Comment

by:ozo
ID: 1255447
typedef s *s;
???
0
 

Author Comment

by:theorange
ID: 1255448
to rbr and trillo

I have read your comments and you are right, I have really misrepresented the simile of the real question that myself and my partner have. (frogland, ICQ API question in the windoze section of experts exchange)

If you study that question (I know you may not have 1st hand experience with this particular piece of software) BUT if you read between the lines you might see how I came to ask this lame one !!

We would really apreciate help here and will gladly pool our points to the provider of a solution !!!!!


 
0
 

Author Comment

by:theorange
ID: 1255449
to rbr and trillo

I have read your comments and you are right, I have really misrepresented the simile of the real question that myself and my partner have. (frogland, ICQ API question in the windoze section of experts exchange)

If you study that question (I know you may not have 1st hand experience with this particular piece of software) BUT if you read between the lines you might see how I came to ask this lame one !!

We would really apreciate help here and will gladly pool our points to the provider of a solution !!!!!


 

0
 

Author Comment

by:theorange
ID: 1255450
Adjusted points to 100
0
 
LVL 10

Expert Comment

by:rbr
ID: 1255453
So what is actually your problem?
0
 

Author Comment

by:theorange
ID: 1255454
rbr
please collect your points on this one
thanks

0
 
LVL 10

Accepted Solution

by:
rbr earned 100 total points
ID: 1255455
thx for the points!
0
 

Expert Comment

by:rsongyl
ID: 1255456
Play around with these

int x[5]={1,2,3,4,5};
int *xptr;
xptr=x;
printf("%d",x[2]);       /*normal array subscript*/
printf("%d",xptr[2]);    /*subscript on pointer*/
printf("%d",*(xptr+2));  /*offset of a pointer*/
printf("%d",*(x+2));     /*offset of the array name*/

All the above statement mean the same thing. Perhaps it can help you understand more.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question