how to allocate memory for this

R

raju

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;;
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

raju said:
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;
}
 
R

raju

thanks ,


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

Default User

raju said:
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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top