simple array question

M

michael

Hi All,

I am trying to create an array the same as the char *argv[] that is passed
to main() ie main(int argc, char *argv[])
I need to store a pointer to and integer in one of the elemets, but the
compiler complains when I try to do this.

a_defined_structure *shared_mem;
char *args[2];

shared_mem = malloc(sizeof (a_defined_structure));

args[0] = argv[1]; //fname
args[1] = shared_mem; // handle

what is the correct method of doing this? I need the two arguments in one
array so I can pass it to execv()

Thanks for your help

Regards

Mike
 
F

firenet

Hi All,

I am trying to create an array the same as the char *argv[] that is passed
to main() ie main(int argc, char *argv[])
I need to store a pointer to and integer in one of the elemets, but the
compiler complains when I try to do this.

a_defined_structure *shared_mem;
char *args[2];

shared_mem = malloc(sizeof (a_defined_structure));

args[0] = argv[1]; //fname
args[1] = shared_mem; // handle
i don't understand you very well.But here shared_mem is pointer to
a_defined_structure and args[1] is pointer
to char.I think you should cast it
 
B

Barry Schwarz

Hi All,

I am trying to create an array the same as the char *argv[] that is passed
to main() ie main(int argc, char *argv[])
I need to store a pointer to and integer in one of the elemets, but the
compiler complains when I try to do this.

a_defined_structure *shared_mem;
char *args[2];

shared_mem = malloc(sizeof (a_defined_structure));

args[0] = argv[1]; //fname
args[1] = shared_mem; // handle

what is the correct method of doing this? I need the two arguments in one
array so I can pass it to execv()

If you want to create an array that stores pointers where the pointers
have different types, then use an array of pointers to void. A
pointer to void can hold any type of object pointer and the language
supports implicit conversions to and from such a pointer. So
void *args[2];
args[0] = argv[1]; /*Your code does check that argv[1] exists?
*/
args[1] = shared_mem; /* Your code does check that malloc
succeeded before it dereferences shared_mem or args[1]? */

Since execv is a non-standard function, you really should tell us what
it's prototype looks like or the answers you get may not help you.


Remove del for email
 
S

Spiros Bousbouras

I am trying to create an array the same as the char *argv[] that is passed
to main() ie main(int argc, char *argv[])
I need to store a pointer to and integer in one of the elemets, but the
compiler complains when I try to do this.
a_defined_structure *shared_mem;
char *args[2];
shared_mem = malloc(sizeof (a_defined_structure));
args[0] = argv[1]; //fname
args[1] = shared_mem; // handle
what is the correct method of doing this? I need the two arguments in one
array so I can pass it to execv()

If you want to create an array that stores pointers where the pointers
have different types, then use an array of pointers to void. A
pointer to void can hold any type of object pointer and the language
supports implicit conversions to and from such a pointer. So
void *args[2];
args[0] = argv[1]; /*Your code does check that argv[1] exists?
*/
args[1] = shared_mem; /* Your code does check that malloc
succeeded before it dereferences shared_mem or args[1]? */

Since execv is a non-standard function, you really should tell us what
it's prototype looks like or the answers you get may not help you.

I will guess that the prototype is
int execv(const char *path, char *const argv[])

Assuming that, I believe that the return value
of malloc() should be assigned to a void * and
this should be assigned to shared_mem and
args[1].
 
S

santosh

michael said:
Hi All,

I am trying to create an array the same as the char *argv[] that is passed
to main() ie main(int argc, char *argv[])
I need to store a pointer to and integer in one of the elemets, but the
compiler complains when I try to do this.

a_defined_structure *shared_mem;

Okay, shared_mem is declared as a pointer to type a_defined_structure.
If a_defined_structure is not a typedef, your declaration should be:

struct a_defined_structure *shared_mem;
char *args[2];

This declares an array of two elements of type pointer to char.
shared_mem = malloc(sizeof (a_defined_structure));

This is better written as:

shared_mem = malloc(sizeof *shared_mem);

This way if the pointed to type of shared_mem changes, this line is
automatically updated.
Also you should check the return value of memory management functions
for failure.
args[0] = argv[1]; //fname
args[1] = shared_mem; // handle

You need to cast shared_mem to pointer to type char before assigning
it to args[1].
 
F

Flash Gordon

santosh wrote, On 12/07/07 07:50:
michael said:
Hi All,

I am trying to create an array the same as the char *argv[] that is passed
to main() ie main(int argc, char *argv[])
I need to store a pointer to and integer in one of the elemets, but the
compiler complains when I try to do this.
args[1] = shared_mem; // handle

You need to cast shared_mem to pointer to type char before assigning
it to args[1].

The OP also needs to ask about his *real* problem on a group where it is
topical, such as comp.unix.programmer, since I suspect attempting to
pass the shared_mem pointer is almost certainly the wrong way to try and
solve his real problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top