Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Pointers to char pointers in functions

Reply
Thread Tools

Pointers to char pointers in functions

 
 
newbie
Guest
Posts: n/a
 
      09-18-2006
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

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      09-18-2006
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.h> included. 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)
 
Reply With Quote
 
 
 
 
newbie
Guest
Posts: n/a
 
      09-18-2006
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.h> included. 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)


 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-18-2006
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
 
Reply With Quote
 
newbie
Guest
Posts: n/a
 
      09-18-2006

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

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-18-2006
newbie wrote:

>
> Default User wrote:


> > Please don't top-post.


> Sorry, understood



Sure, thanks.




Brian
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-18-2006
"newbie" <(E-Mail Removed)> writes:
> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      09-23-2006
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> wrote:

>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 (E-Mail Removed)
[S]uch compilers usually allow <code samples> respectively 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-23-2006
(E-Mail Removed) (Dave Vandervies) writes:
> In article <(E-Mail Removed)>,
> Keith Thompson <(E-Mail Removed)> wrote:
>
>>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) (E-Mail Removed) <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.
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      09-24-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
char *fred; char * fred; char *fred; any difference? Ben Pfaff C Programming 5 01-17-2004 07:37 PM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments