Passing String to Function

J

jconnort

I'm sure this is newbie but I've done some searching on this and
haven't found the exact same situation. Basically, I'm trying to pass a
string to a function where it get's built up (it's supposed to be a
where clause for a a database query) and in the called function it
get's built up with malloc as necessary. The problem is, I can't get it
to have the proper value when it comes back to the called function.
Initially, it's just a pointer to char. Can anyone point me to an FAQ
or some such ? Thanks...
 
F

Flash Gordon

I'm sure this is newbie but I've done some searching on this and
haven't found the exact same situation. Basically, I'm trying to pass a
string to a function where it get's built up (it's supposed to be a
where clause for a a database query) and in the called function it
get's built up with malloc as necessary. The problem is, I can't get it
to have the proper value when it comes back to the called function.
Initially, it's just a pointer to char. Can anyone point me to an FAQ
or some such ? Thanks...

Try changing the * to a ** on line 42. Or, in other words, how can we
tell you what you have done wrong if you don't show us what you have done?

You might find this FAQ useful though:
http://c-faq.com/ptrs/passptrinit.html
 
M

Mark McIntyre

I'm sure this is newbie but I've done some searching on this and
haven't found the exact same situation. Basically, I'm trying to pass a
string to a function where it get's built up

Post some sample of your code. Its impossible to diagnose it via
crystal ball...
Mark McIntyre
 
C

clayne

If you're building up a new block block of data based on the contents
of the passed pointer, you shouldn't be using locally defined
(automatic) variables. Instead you should be using malloc to allocate
memory from the heap and return a pointer to the newly allocated area;
or preferably pass an additional argument to your function that is the
address of memory you've allocated for it to store it's results in. The
former lends itself to memory leaks if you're not diligent whereas the
latter usually goes along with better memory management practices
(doesn't mean you are immune to leaking if you forget to free() it as
well, however).
 

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

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,476
Latest member
FredericCo

Latest Threads

Top