how to allocate memory for this

Discussion in 'C Programming' started by raju, Jan 12, 2006.

  1. raju

    raju Guest

    hello all,

    i want to know about dynamically allocating for
    char **ptr

    and in problem
    #include<stdio.h>
    main()
    {
    char **p={"hello","world"};

    printf("%s",*ptr);
    printf("%c",**ptr);
    printf("%s",*(ptr+1));

    }

    i wann to print "hello" , "h", "world"

    is this correct code , is ther any problem with this code;

    well i know while initializing tha 2d char array
    char *p[]={"hello","world"};
    but i want to know to how i can initialize using pointer to ponter;;
    raju, Jan 12, 2006
    #1
    1. Advertising

  2. raju wrote:
    > hello all,
    >
    > i want to know about dynamically allocating for
    > char **ptr
    >
    > and in problem
    > #include<stdio.h>
    > main()
    > {
    > char **p={"hello","world"};
    >
    > printf("%s",*ptr);
    > printf("%c",**ptr);
    > printf("%s",*(ptr+1));
    >
    > }
    >
    > i wann to print "hello" , "h", "world"
    >
    > is this correct code , is ther any problem with this code;
    >
    > well i know while initializing tha 2d char array
    > char *p[]={"hello","world"};
    > but i want to know to how i can initialize using pointer to ponter;;
    >


    Maybe this code will help?

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>

    int main(void)
    {
    char** p;

    /* First of all we need memory to store the two pointers */
    if( (p = malloc(sizeof *p * 2)) == NULL)
    return EXIT_FAILURE;


    /* Now we need memory to store the actual strings.
    * There are two strings, "hello" and "world".
    * We just allocate 100 bytes to each string, to keep things simple.
    */
    if( (*p = malloc(100)) == NULL
    || (*(p + 1) = malloc(100)) == NULL)
    return EXIT_FAILURE;


    /* Now copy some values to the strings */
    strcpy(p[0], "hello");
    strcpy(p[1], "world");

    /* And print the contents */
    printf("%s\n", *p);
    printf("%c\n", **p);
    printf("%s\n", *(p + 1));

    /* Free mem and exit */
    free(p[0]);
    free(p[1]);
    free(p);
    return EXIT_SUCCESS;
    }
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Jan 12, 2006
    #2
    1. Advertising

  3. raju

    raju Guest

    thanks ,


    and how can i initialize the
    char **p
    is it possible to initialize using the pointer to pointers;
    raju, Jan 13, 2006
    #3
  4. raju

    Default User Guest

    raju wrote:

    > thanks ,
    >
    >
    > and how can i initialize the
    > char **p
    > is it possible to initialize using the pointer to pointers;


    What are you talking about?



    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Jan 13, 2006
    #4
    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. Roedy Green
    Replies:
    3
    Views:
    3,239
    Roedy Green
    Aug 14, 2003
  2. sks_cpp
    Replies:
    7
    Views:
    378
    Jerry Coffin
    Jul 4, 2003
  3. Curt
    Replies:
    37
    Views:
    1,163
    Alexander Terekhov
    Jul 26, 2003
  4. OlgaM
    Replies:
    1
    Views:
    395
    Mike Wahler
    Oct 10, 2003
  5. Alan Gifford
    Replies:
    4
    Views:
    383
    tom_usenet
    Oct 27, 2003
Loading...

Share This Page