Solved

Newbie question about  structs

Posted on 1998-12-27
13
221 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
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!

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Test against App 49 137
TCP/IP Socket - connection close results in data lost 14 132
Problem to ASCII 1 176
Windows Updates failing due to Diskpart not configured correctly 8 117
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…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

813 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

10 Experts available now in Live!

Get 1:1 Help Now