Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Newbie question about  structs

Posted on 1998-12-27
13
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 200 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

Independent Software Vendors: 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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

636 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