Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   how to allocate memory for this (http://www.velocityreviews.com/forums/t440864-how-to-allocate-memory-for-this.html)

raju 01-12-2006 08:35 AM

how to allocate memory for this
 
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?= 01-12-2006 09:03 AM

Re: how to allocate memory for this
 
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;
}


raju 01-13-2006 06:33 AM

Re: how to allocate memory for this
 
thanks ,


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


Default User 01-13-2006 06:14 PM

Re: how to allocate memory for this
 
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.


All times are GMT. The time now is 07:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57