473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

using function getting the address of string into a pointer

char * get();
void main()
{
char*c;
c=get();
printf("%s",c);
}
char * get()
{
char ch[3]="km";
return(ch);
}

the string is not getting dereferenced by the pointer 'c',
i.e the function is not returning the address of string
Apr 1 '10 #1
3 2021
Banfa
9,065 Expert Mod 8TB
The function is returning the address of the string. Unfortunately since the string has automatic storage scope as soon as the function returns it is destroyed so the pointer returned is invalid.
Apr 1 '10 #2
so how can i can get the string from a function as such???
Apr 1 '10 #3
Banfa
9,065 Expert Mod 8TB
Well you have 3 options in C basically
  1. Return a pointer to static data. However this is not re-entrant or thread safe and is generally considered a bad idea.
  2. Return a pointer to data that has been malloc'd, but then you need to be sure to free the returned pointer once you have finished with it or you get a memory leak. The potential for memory leaks makes this not a brilliant idea either but it is sometimes useful.
  3. Don't return a pointer, instead pass in a pointer to a location to store the result in from the external program along with a number indicating the size of the passed in buffer. Return a boolean (0 or 1) value indicating if the supplied buffer was large enough or an integer indicating the required size of the buffer if it was not large enough.
Apr 1 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
7
by: Prashanth Badabagni | last post by:
#include<string.h> void (*foobar)(); void main() { char *str; strcpy(str,"Fun1"); strcat(str,"method1");
17
by: Johs32 | last post by:
When I make a pointer I have read that if I would like to use it in another function, I need to malloc it first else it will disapear after the function returns. In this code I do not use malloc,...
5
by: RKS | last post by:
I am new to Com programming using C++. Can pointers be passed through interface functions. I would like to pass multi dimensional arrays to functions defined in the com interface. Any help is...
3
by: lars.uffmann | last post by:
Hi everyone! I am debugging a big piece of code on the search for memory leaks, using g++ under suse 9.3. Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a class foo that is...
6
by: semkaa | last post by:
Can you explain why using ref keyword for passing parameters works slower that passing parameters by values itself. I wrote 2 examples to test it: //using ref static void Main(string args) {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.