Array of Pointers and Freeing problem

Discussion in 'C Programming' started by anand1603@yahoo.com, May 12, 2007.

  1. Guest

    hi,
    Following is problematic Code

    main()
    {
    char *ptr[1]={NULL};
    ftn(ptr);


    }
    void ftn(**data)
    {
    *data=malloc(4);
    memset(*data,0,4);
    strcpy(*data,"HAI");
    }
     
    , May 12, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > hi,
    > Following is problematic Code
    >

    Indeed it is, it won't compile.

    > main()
    > {
    > char *ptr[1]={NULL};
    > ftn(ptr);
    >
    >
    > }
    > void ftn(**data)
    > {
    > *data=malloc(4);
    > memset(*data,0,4);
    > strcpy(*data,"HAI");
    > }
    >



    --
    Ian Collins.
     
    Ian Collins, May 12, 2007
    #2
    1. Advertising

  3. said:

    > hi,
    > Following is problematic Code
    >
    > main()
    > {
    > char *ptr[1]={NULL};
    > ftn(ptr);
    >
    >
    > }
    > void ftn(**data)
    > {
    > *data=malloc(4);
    > memset(*data,0,4);
    > strcpy(*data,"HAI");
    > }


    This won't compile either.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, May 12, 2007
    #3
  4. wrote:
    > hi,
    > Following is problematic Code


    Yes, it is.

    Start by paying attention to the following changes. Then decide what I
    have left undone.

    /* You need these headers */
    #include <stdlib.h>
    #include <string.h>

    /* Without the following, ftn (in main) is explicitly declared to
    return an int, which conflicts with its later redefinition */
    void ftn(char *data[]);

    /* main returns an int. You should say so. Not doing so is an error
    for the C standard from 1999 on, and a bad idea for the earlier
    standard. */
    int main(void)
    {
    char *ptr[1] = { NULL };
    ftn(ptr);

    /* You allocated space for ptr[0] to point at. free it */
    free(*ptr);

    /* main returns an int. You should do so. Not doing so is an error
    for the C standard up to 1999, and a bad idea for the later
    standard. */
    return 0;

    }

    /* You have specified no type for the argument to ftn, do so: */
    void ftn(char *data[])
    {
    *data = malloc(4);
    /* you should always check that malloc succeeds */
    if (!*data)
    return;
    #if 0
    /* The following memset is completely worthless. It has been
    commented out. */
    memset(*data, 0, 4);
    #endif
    strcpy(*data, "HAI");
    }
     
    Martin Ambuhl, May 12, 2007
    #4
  5. On Sat, 12 May 2007 18:07:04 -0400, CBFalconer <>
    wrote:

    >Richard Heathfield wrote:
    >> said:
    >>
    >>> Following is problematic Code
    >>>
    >>> main()
    >>> {
    >>> char *ptr[1]={NULL};
    >>> ftn(ptr);
    >>> }
    >>>
    >>> void ftn(**data)
    >>> {
    >>> *data=malloc(4);
    >>> memset(*data,0,4);
    >>> strcpy(*data,"HAI");
    >>> }

    >>
    >> This won't compile either.

    >
    >However, this will (untested). (To OP) Study the difference.
    >
    >#include <stdlib.h>
    >#include <string.h>
    >
    >void ftn(**data) {


    Doesn't the argument need a type? I think you meant

    void ftn(char **data){

    > *data = malloc(4);
    >
    > memset(*data, 0, 4);
    > strcpy(*data, "HAI");
    >}
    >
    >int main(void) {
    > char *ptr[1] = {NULL};
    > ftn(ptr);
    > return 0;
    >}
    >



    Remove del for email
     
    Barry Schwarz, May 13, 2007
    #5
    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. countofquad
    Replies:
    4
    Views:
    377
    countofquad
    Mar 5, 2005
  2. Amogh
    Replies:
    5
    Views:
    414
    Simon Biber
    May 3, 2006
  3. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    682
  4. szimek
    Replies:
    0
    Views:
    121
    szimek
    Jun 22, 2009
  5. G G
    Replies:
    4
    Views:
    96
Loading...

Share This Page