By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,966 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,966 IT Pros & Developers. It's quick & easy.

Pointers to char pointers in functions

P: n/a
Can someone please give me some tips?

I have a function f(). In function f() I allocate memory for a char
array. Now the memory I allocate in f() must be assigned to a char*
outside of f() so how would I pass in a char* and assign it the address
of the allocated memory, example:

void f(char* p)
{
char* a=(char* )malloc(10);
// do some error checking;
return(0);
}

int main(void)
{
char *b;

// call f()...

return(0);
}

How would I call f() so that afterwards b, in main(), points to the
memory allocated in function f()? Do I need to change the
declaration/definition of f() to something like:

void f(char** p)...

Or other and how would I call f()? Please note this is not any
howework or such I am purely learning C in my spare time.

Anything appreciated.

Steve

Sep 18 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
newbie said:
Can someone please give me some tips?

I have a function f(). In function f() I allocate memory for a char
array. Now the memory I allocate in f() must be assigned to a char*
outside of f() so how would I pass in a char* and assign it the address
of the allocated memory, example:

void f(char* p)
{
char* a=(char* )malloc(10);
// do some error checking;
return(0);
}
Not like that!

Firstly, drop your cast on malloc. It's not required, and can conceal a bug.
If your compiler seems to be telling you that it /is/ required, check that
you've got <stdlib.hincluded. Chances are you haven't. Include it, and
the warning message will go away. If it doesn't, then you're accidentally
using a C++ compiler instead of a C compiler.

Secondly, let's answer your question. Here's the simple way:

#include <stdlib.h>

char *f(void)
{
return malloc(10);
}

int main(void)
{
char *b = f();
if(b != NULL)
{
/* use b for whatever you wanted 10 bytes of memory for, and then... */
free(b);
}
return 0;
}

And here's the less simple way:

#include <stdlib.h>

void f(char **a) /* note the >>>two<<< stars */
{
*a = malloc(10);
}

int main(void)
{
char *b = NULL;
f(&b);

if(b != NULL)
{
/* use b for whatever you wanted 10 bytes of memory for, and then... */
free(b);
}
return 0;
}
<snip>
How would I call f() so that afterwards b, in main(), points to the
memory allocated in function f()? Do I need to change the
declaration/definition of f() to something like:

void f(char** p)...
Yes. See above.
Or other and how would I call f()?
Again, see above.
Please note this is not any
howework or such I am purely learning C in my spare time.
In this case, it wouldn't have mattered if it were your homework, since it
was clear from your question that you were trying to understand, and had
made a fair effort on your own account. This newsgroup is not against
helping with homework. It's against laziness and dishonesty. You failed to
meet the qualifying criteria for being an Enemy of comp.lang.c. ;-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 18 '06 #2

P: n/a
Many thanks for the response Richard. I will from now on drop the
cast's for malloc unless the compiler complains (I generally use MS's
cl). I do include stdlib.h when using malloc I have just missed it
from my example (sorry about that).

Thanks for such a prompt response!

Richard Heathfield wrote:
newbie said:
Can someone please give me some tips?

I have a function f(). In function f() I allocate memory for a char
array. Now the memory I allocate in f() must be assigned to a char*
outside of f() so how would I pass in a char* and assign it the address
of the allocated memory, example:

void f(char* p)
{
char* a=(char* )malloc(10);
// do some error checking;
return(0);
}

Not like that!

Firstly, drop your cast on malloc. It's not required, and can conceal a bug.
If your compiler seems to be telling you that it /is/ required, check that
you've got <stdlib.hincluded. Chances are you haven't. Include it, and
the warning message will go away. If it doesn't, then you're accidentally
using a C++ compiler instead of a C compiler.

Secondly, let's answer your question. Here's the simple way:

#include <stdlib.h>

char *f(void)
{
return malloc(10);
}

int main(void)
{
char *b = f();
if(b != NULL)
{
/* use b for whatever you wanted 10 bytes of memory for, and then... */
free(b);
}
return 0;
}

And here's the less simple way:

#include <stdlib.h>

void f(char **a) /* note the >>>two<<< stars */
{
*a = malloc(10);
}

int main(void)
{
char *b = NULL;
f(&b);

if(b != NULL)
{
/* use b for whatever you wanted 10 bytes of memory for, and then... */
free(b);
}
return 0;
}
<snip>
How would I call f() so that afterwards b, in main(), points to the
memory allocated in function f()? Do I need to change the
declaration/definition of f() to something like:

void f(char** p)...

Yes. See above.
Or other and how would I call f()?

Again, see above.
Please note this is not any
howework or such I am purely learning C in my spare time.

In this case, it wouldn't have mattered if it were your homework, since it
was clear from your question that you were trying to understand, and had
made a fair effort on your own account. This newsgroup is not against
helping with homework. It's against laziness and dishonesty. You failed to
meet the qualifying criteria for being an Enemy of comp.lang.c. ;-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 18 '06 #3

P: n/a
newbie wrote:
Many thanks for the response Richard.
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>


Brian
Sep 18 '06 #4

P: n/a

Default User wrote:
newbie wrote:
Many thanks for the response Richard.

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>


Brian
Sorry, understood :-)

Sep 18 '06 #5

P: n/a
newbie wrote:
>
Default User wrote:
Please don't top-post.
Sorry, understood :-)

Sure, thanks.


Brian
Sep 18 '06 #6

P: n/a
"newbie" <st*****@wvickers.freeserve.co.ukwrites:
Many thanks for the response Richard. I will from now on drop the
cast's for malloc unless the compiler complains (I generally use MS's
cl). I do include stdlib.h when using malloc I have just missed it
from my example (sorry about that).
And if the compiler *does* complain, stop and figure out why it's
complaining before you add a cast to shut it up.

Never add a cast for the sole purpose of inhibiting a compiler
warning. A cast effectively tells the compiler, "trust me, I know
what I'm doing". Inserting a cast when you *don't* really know what
you're doing is an excellent way to shoot yourself in the foot.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 18 '06 #7

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>Never add a cast for the sole purpose of inhibiting a compiler
warning. A cast effectively tells the compiler, "trust me, I know
what I'm doing". Inserting a cast when you *don't* really know what
you're doing is an excellent way to shoot yourself in the foot.
A more memorable way of putting this is:

The cast is how you tell the compiler "I know that's my foot, but I'm
very carefully aiming between the toes."
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
[S]uch compilers usually allow <code samplesrespectively as "please don't
nag, I _know_ that's my foot, but I'm very carefully aiming between the toes."
--Alan Bellingham in the scary devil monastery
Sep 23 '06 #8

P: n/a
dj******@caffeine.csclub.uwaterloo.ca (Dave Vandervies) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>>Never add a cast for the sole purpose of inhibiting a compiler
warning. A cast effectively tells the compiler, "trust me, I know
what I'm doing". Inserting a cast when you *don't* really know what
you're doing is an excellent way to shoot yourself in the foot.

A more memorable way of putting this is:

The cast is how you tell the compiler "I know that's my foot, but I'm
very carefully aiming between the toes."
I like it!

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 23 '06 #9

P: n/a
newbie wrote:
Can someone please give me some tips?

I have a function f(). In function f() I allocate memory for a char
array. Now the memory I allocate in f() must be assigned to a char*
outside of f() so how would I pass in a char* and assign it the address
of the allocated memory,
If the sole purpose of your function is to do checked memory allocation
I would define a generic function, something like:

#define NEW(p, n) \
p = malloc(n); \
if (p == NULL) { \
/* do something */ \
}

If not, I think the most flexible solution is to to let the caller
provide a large enough character array instead of allocating memory
inside the function:

void f(char *result)
{
/* Assign values to the array pointed at by result. */
}
August
Sep 24 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.