473,387 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How can I pass an array of pointers to stuctures?

Does anyone know how to return a usable array of pointers to structures?

Here's the structure:
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.     char* name;   //  '\0'-terminated C string 
  3.     int   number;
  4. } SomeSeq;
  5.  
I know I can do this:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     ...
  3.     int n = 2;
  4.     SomeSeq myStruct[n];
  5.     myStruct[0].name = "Ric\0";
  6.     myStruct[0].number = 5;
  7.     ...
  8.  
Now I want to create a function that returns myStruct[n] where 'n' is variable.
Here's my attempt:

Expand|Select|Wrap|Line Numbers
  1. struct *myFunction() {
  2.     .... // computing the value of 'n'...
  3.     return myStruct[n];
  4. }
  5.  
I'm not quite sure of the syntax here.

Regards,

Ric (lost Neophyte)
Aug 30 '07 #1
2 1203
RRick
463 Expert 256MB
In your function, myStruct[n] refers to a SomeSeq, not a SomeSeg*. To return a pointer to SomeSeq, define the function as:
Expand|Select|Wrap|Line Numbers
  1. struct SomeSeq *  myFunction()
and have the function
Expand|Select|Wrap|Line Numbers
  1. return & myStruct[n];
Aug 31 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
To the OP:

Be sure you don't return the address of a local variable since that variable will be destroyed when the function completes.
Sep 3 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
8
by: Blue Ocean | last post by:
I know this is somewhat dependent on the circumstances, but let me ask anyway. Suppose I have a 100 byte struct or array or something like that. Which would be more efficient? void...
3
by: questions? | last post by:
I tried to pass a two dimensional array in the function arguments the following program is a demonstration, ******************************************** # include <stdio.h> # include...
12
by: FI | last post by:
Hello All, I am relatively new to C programming and I am struck with a problem in dynamic memory allocation. I would like to know if it is ok to pass the 'memory address' returned by...
0
by: verpeter | last post by:
When try pass only one delegate as parameter, all work fine. But I need call function, that take as parameter array of function pointers. From C++ code I used such code: static void* aFuncPtrs =...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.