Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Difference betw...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 12 Topic 4256 of 4370
Post > Topic >>

Difference between pointer-to-pointer and pointer

by Pranav Negandhi <pranav@[EMAIL PROTECTED] > Aug 31, 2008 at 11:16 AM

I understand that a char buffer is passed into a function when it has to 
return a string. What I am not very clear about is why does it need a 
char** and not a char* directly.

If memory is allocated for the buffer in either case using malloc, 
shouldn't it work the same? A short working example is pasted below with 
two functions, one of which takes a char** and the other char*.

Appreciate the help.

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

void getname(char **);
void getname2(char *);

int main(void)
{
	char *c = NULL;
	char *d = NULL;
	
	getname(&c);
	printf("char    - %d\t\t%d\n", *c, sizeof *c);
	printf("char *  - %d\t%d\n", c, sizeof c);
	printf("char ** - %d\t%d\n", &c, sizeof &c);
	printf("---\n");
	
	getname2(d);
	printf("char    - %d\t\t\n", *d); // Crashes here
	printf("char *  - %d\t%d\n", d, sizeof d);
	printf("char ** - %d\t%d\n", &d, sizeof &d);
}

void getname(char **c)
{
	char s[] = "Oranges and Lemons sold for a penny";
	
	*c = malloc(10 * sizeof **c);
	sprintf(*c, "%s", s);
}

void getname2(char *d)
{
	char s[] = "Oranges and Lemons sold for a penny";
	
	d = malloc(10 * sizeof *d);
	sprintf(d, "%s", s);
	printf("char    - %d\t\t%d\n", *d, sizeof *d);
}

Regards,
Pranav Negandhi
www.pranavnegandhi.com
 




 12 Posts in Topic:
Difference between pointer-to-pointer and pointer
Pranav Negandhi <prana  2008-08-31 11:16:20 
Re: Difference between pointer-to-pointer and pointer
Richard Heathfield <rj  2008-08-31 06:03:38 
Re: Difference between pointer-to-pointer and pointer
Pranav Negandhi <prana  2008-09-01 14:30:13 
Re: Difference between pointer-to-pointer and pointer
Richard Heathfield <rj  2008-09-01 09:15:59 
Re: Difference between pointer-to-pointer and pointer
Barry Schwarz <schwarz  2008-09-01 12:14:44 
Re: Difference between pointer-to-pointer and pointer
Pranav Negandhi <prana  2008-09-02 11:43:50 
Re: Difference between pointer-to-pointer and pointer
Ian Collins <ian-news@  2008-08-31 18:03:30 
Re: Difference between pointer-to-pointer and pointer
Aggro <spammerdream@[E  2008-08-31 06:50:49 
Re: Difference between pointer-to-pointer and pointer
LR <lruss@[EMAIL PROTE  2008-08-31 03:44:56 
Re: Difference between pointer-to-pointer and pointer
Barry Schwarz <schwarz  2008-08-31 03:06:12 
Re: Difference between pointer-to-pointer and pointer
LR <lruss@[EMAIL PROTE  2008-08-31 15:24:20 
Re: Difference between pointer-to-pointer and pointer
Barry Schwarz <schwarz  2008-08-31 03:06:12 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 8:38:33 CST 2008.