473,387 Members | 1,512 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,387 software developers and data experts.

Passing strings to and from functions

I am trying to pass a string to a Mid() function, return a portion of the string, and then convert that portion to an int. My attempt looks like this..

char * ReceivedStr;
char * Mid(char * inString, int Start, int Length );


main()
{
int i;
int dis;
char * Distance;
ReceivedStr = "Move125";

Distance = Mid(ReceivedStr, 4, 3);
dis = atoi(Distance);
}

//This function should return the middle section of a given string…
char * Mid(char * inString, int Start, int Length )
{
char outString[50];
int i, iOut;
iOut = 0;
for (i = Start; i < Start + Length; i++){
outString[iOut] = inString[i];
iOut++;
}
outString[Length] = '\0';
return outString;
}

Right before the mid function returns the value of outString is "152", but for some reason the line Distance = Mid(ReceivedStr, 4, 3); does not set Distance to "152".
Could someone please explain what I'm doing wrong?
Sep 11 '09 #1
3 1818
Banfa
9,065 Expert Mod 8TB
You are returning a pointer to local data. That is the function Mid returns a pointer to its own internal buffer outString. However as soon as Mid returns the data allocated to outString is released (normally back to the stack).

Never, ever return a pointer to local data. Either pass a pointer with a location to write to in the function parameter list or dynamically allocate the return buffer, but then you will have to free it too.

You could declare outString static, this makes the variable persist even after the function Mid has returned. However this is very poor practice and as soon as you get to writing multi-threaded applications you would have to use a different scheme because that is not re-entrant.
Sep 11 '09 #2
...I see. That makes sense. Thank you!
Sep 11 '09 #3
newb16
687 512MB
You return address of the temporary (allocated on stack during function call) variable outString. Its content then may be destroyed in any time, e.g. in call to atoi. Allocate memory for it using malloc or make the calling function pre-alocate buffer.
Sep 11 '09 #4

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

Similar topics

4
by: Suddn | last post by:
Help me get my mind around passing string types to a function. I need to have the function modify the string types and get them back. Normaly I would just return the modified string but I need to...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
9
by: Colin Doig | last post by:
Hi, I need to pass an array of strings to a function, but I can't get it to work. This is what I wrote : #include <stdio.h> void a_function(char *blah) { printf("%s %s %s", blah, blah,...
2
by: MJL | last post by:
I am working on a small project that involves the manipulation of dynamically allocated memory for strings. I was wondering why the string.h functions are the way they are and why not as follows:...
1
by: Ivan Sergio Borgonovo | last post by:
I can't understand how to pass strings to functions. create type tSession as ( SessionCode char(32), SessionID char(32), UserIDI integer, SessionN integer );
11
by: Macca | last post by:
Hi, I'm writing an application that will pass a large amount of data between classes/functions. In C++ it was more efficient to send a pointer to the object, e.g structure rather than passing...
61
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With Nothing I assume no memory is set aside nor GC'ed But...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
13
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.