Solved

Newbie question about  structs

Posted on 1998-12-27
13
204 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

759 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

21 Experts available now in Live!

Get 1:1 Help Now