Solved

Newbie question about  structs

Posted on 1998-12-27
13
214 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
 
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
Save on storage to protect fatherhood memories

You're the dad who has everything. This Father's Day, make sure your family memories are protected. My Passport Ultra has automatic backup and password protection to keep your cherished photos and videos safe. With up to 3TB, you have plenty of room to hold the adventures ahead.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now