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

x
?
Solved

Referencing an array structure element with a pointer

Posted on 2003-10-26
5
Medium Priority
?
338 Views
Last Modified: 2010-04-15
I would like to know how to reference an array in a function where the array of structures has been passed as a pointer.

If I have a structure:
typedef{
UInt32   A;
UInt16   B;
Char       C[5];
} Stuff

and I create an array:
  Stuff    myStuff[10];

and then pass a pointer to a routine:
Routine(&myStuff);

where the routine takes the pointer:
void Routine(Stuff *pStuff)
{

// do stuff

}

How do I reference say the 9th item A in the array myStuff from within the function Routine?

Thanks

Greg
0
Comment
Question by:trifusion
[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
5 Comments
 
LVL 15

Accepted Solution

by:
efn earned 1000 total points
ID: 9622927
The usual way to do this is to pass the address of the first element of the array, not the address of the array.  If you just pass the array as the argument, it automatically gets converted to a pointer to the first element.

Routine(myStuff);

Then the function can be declared either as you have it or like this:

void Routine(Stuff pStuff[])

The two forms are equivalent.

Inside the function, you can use the argument as an array name:

pStuff[8].A = 75;

--efn
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9624861
Hi Greg...

  you can access your array by using the following code....

typedef struct{
short   A;
int     B;
char    C[5];
} Stuff ;

 
Stuff myStuff[10];

void Routine(Stuff *pStuff)
{
     for(int i = 0; i < 10; i++){     // you can access a pointer as an array and vice-versa
          printf("%d",  pStuff[i].B );
      }
}


int main()
{
  int i ;

  for(i = 0; i < 10; i++){
        myStuff[i].B = i + 1;
  }

  Routine(myStuff);
  return 0 ;
}



Dennis
0
 
LVL 6

Expert Comment

by:Ajar
ID: 9625394
trifusion,

Because the array myStuff has been statically allocated memory ..i.e  
>> Stuff    myStuff[10];
instead of
>> Stuff   *myStuff  = (Stuff *) malloc(10* sizeof(Stuff));

it will be wise to declare the function Routine as
====================================
 void (Stuff  pStuff[] )
{
    // check if the size of pstuff is enough for accessing 9th element
    if( sizeof(pstuff)/sizeof(stuff)  >9)  
    { // ACCESS THE 9th element as
      pStuff[8].A = 75
    }
     else
    //   BE AFRAID THAT you will do illegal memory access and so quietly return
    return;
}
====================================
Basically    if you pass the pstuff   as    "Stuff  pStuff[]"   to a function then the function
can check the array size by using 'sizeof ()' .

where as  if you pass  'Stuff * pStuff' .. your function has no way to ensure that there wont be illegal access unless you
also pass the array length along with the pointer

void (Stuff  pStuff[] , int pStuff_len)
{
////Do something.
}


0
 
LVL 15

Expert Comment

by:efn
ID: 9625486
> Basically    if you pass the pstuff   as    "Stuff  pStuff[]"   to a function then the function
> can check the array size by using 'sizeof ()' .

I am inclined to doubt this assertion.  sizeof is a compile-time operation.  When the compiler compiles a function definition with an array argument of unspecified size, the compiler does not know what will be passed to the function at run time, so it can't give you the size of the array.  What it does give you is the size of the pointer that is actually passed.

I tested this with Microsoft Visual C++ 6.0 and Borland C++Builder 4.  With both compilers, when I passed an array of 17 integers, the called function reported the size as 4, the size of a pointer.

--efn
0
 

Author Comment

by:trifusion
ID: 9631649
All the comments I am sure are valid, but I am self-taught C and I really was just looking for the correct syntax for describing the array once passed as a pointer to a function.

I appreciate the memory notes, I am programming in Palm OS and so I am very aware of the memory issues.

Thanks to all who replied.

Greg
0

Featured Post

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.

Question has a verified solution.

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

609 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