simple array question

Discussion in 'C Programming' started by michael, Jul 12, 2007.

  1. michael

    michael Guest

    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
    michael, Jul 12, 2007
    #1
    1. Advertising

  2. michael

    firenet Guest

    On Jul 12, 10:32 am, "michael" <> wrote:
    > 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
    >
    > 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
    firenet, Jul 12, 2007
    #2
    1. Advertising

  3. On Thu, 12 Jul 2007 10:32:55 +0800, "michael" <> wrote:

    >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
    Barry Schwarz, Jul 12, 2007
    #3
  4. On 12 Jul, 04:31, Barry Schwarz <> wrote:
    > On Thu, 12 Jul 2007 10:32:55 +0800, "michael" <> wrote:
    > >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.


    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].
    Spiros Bousbouras, Jul 12, 2007
    #4
  5. michael

    santosh Guest

    michael wrote:
    > 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].
    santosh, Jul 12, 2007
    #5
  6. michael

    Flash Gordon Guest

    santosh wrote, On 12/07/07 07:50:
    > michael wrote:
    >> 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.


    <snip>

    >> 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.
    --
    Flash Gordon
    Flash Gordon, Jul 12, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    545
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    649
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    825
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    361
    Roland Hall
    Jan 26, 2004
  5. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    203
    Peter Bailey
    Apr 8, 2008
Loading...

Share This Page