473,499 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String questions

Hi all,

I've been designing a program which would need to have a function to
replace values in a string.
For example I could have a string:
"<circle x="%" y="%">" and then I would replace the % with actual
values. But I cannot think of a way to convert an integer(say 5643) to
a NULL terminated string. I'm thinking of writing a function like:
char* get_circle(int x, int y)
{
}

But that would return a char* and not the actual string. So I would
need to dynamically allocate the string(not on the stack) so it is not
deleted automatically after the function ends. Then the code which is
using the result is responsible to free it, isn't it? Any ideas how to
go about implementing this function? I'm not asking for code, just for
directions. Thanks.

PS.
Say you use some function which returns a char* and you have some code
like:
char* c;
c = somefun();
Then surely somefun(); returns a char* which is saved in memory
temporarily and then this is assigned to c. But where does the
temporary value go? Who's responsible for freeing it?

Feb 14 '06 #1
4 1587
"gamehack" <ga******@gmail.com> writes:
I've been designing a program which would need to have a function to
replace values in a string.
For example I could have a string:
"<circle x="%" y="%">" and then I would replace the % with actual
values.
C already has a function that does something similar to that. It
is call sprintf().
But I cannot think of a way to convert an integer(say 5643) to
a NULL terminated string.
You can also do that with sprintf().
I'm thinking of writing a function like: char* get_circle(int
x, int y) {
}

But that would return a char* and not the actual string.
I'm puzzled how a char *, that presumably points to the start of
a string, is not an actual string.
So I would need to dynamically allocate the string(not on the
stack) so it is not deleted automatically after the function
ends. Then the code which is using the result is responsible to
free it, isn't it?
Yes.
Any ideas how to go about implementing this function? I'm not
asking for code, just for directions. Thanks.
Call malloc() to get an appropriate amount of space.
Use sprintf() to write into the space[*].
Return the string.
[*] snprintf() is a safer choice but not all implementations have
it, because it is new in C99.

An alternative approach would be to have the caller provide a
buffer into which the string is written. This can in many cases
avoid the need for dynamic memory allocation, because the caller
is often able to allocate automatic memory (which is typically
"on the stack", as you say) for the string.
PS.
Say you use some function which returns a char* and you have some code
like:
char* c;
c = somefun();
Then surely somefun(); returns a char* which is saved in memory
temporarily and then this is assigned to c. But where does the
temporary value go? Who's responsible for freeing it?


Typically, the caller is responsible for freeing it, but you can
design your code to work however you like.
--
"I should killfile you where you stand, worthless human." --Kaz
Feb 14 '06 #2


gamehack wrote On 02/14/06 14:55,:
Hi all,

I've been designing a program which would need to have a function to
replace values in a string.
For example I could have a string:
"<circle x="%" y="%">" and then I would replace the % with actual
values. But I cannot think of a way to convert an integer(say 5643) to
a NULL terminated string. I'm thinking of writing a function like:
char* get_circle(int x, int y)
{
}
Is there some reason you can't use sprintf(), or
snprintf() if you have a C99 implementation?
But that would return a char* and not the actual string. So I would
need to dynamically allocate the string(not on the stack) so it is not
deleted automatically after the function ends. Then the code which is
using the result is responsible to free it, isn't it? Any ideas how to
go about implementing this function? I'm not asking for code, just for
directions. Thanks.
Objects on the stack (more precisely, "objects with
automatic storage duration") disappear when execution
leaves the block that contains them. So you're right:
it would be folly to return a pointer to such an object,
because the object would vanish before the caller ever
got an opportunity to use the pointer to it.

Some other possibilities:

- The caller could provide the buffer and let the
called function store the string in it. Issues:
the caller must know how large a buffer to provide,
or else the called function needs a way to tell
the caller that the buffer was too small.

- The called function could allocate the storage
dynamically with malloc() or equivalent. Issues:
what should the function do if malloc fails, and
who takes responsibility for freeing the memory
when it's no longer needed?

- The string could be placed in static storage, which
can be pointed to safely by a returned value because
static storage outlasts the execution of any single
block. Issues: You can only have one string "in
play" at a time, because it will be overwritten the
next time the function is called. Variations: the
string could be in dynamic storage with the pointer
to it being static, the static area could have enough
"slots" for N strings used in round-robin fashion to
allow up to N circles to be "in play" simultaneously.
PS.
Say you use some function which returns a char* and you have some code
like:
char* c;
c = somefun();
Then surely somefun(); returns a char* which is saved in memory
temporarily and then this is assigned to c. But where does the
temporary value go? Who's responsible for freeing it?


The value returned by a function is just a value; it
may or may not be stored in memory as part of the value-
returning mechanism for functions. For example, it might
reside in a designated CPU register. Whatever mechanism
is used, it is the C implementation's responsibility to
handle the details, not the programmer's.

--
Er*********@sun.com

Feb 14 '06 #3

Eric Sosman wrote:
gamehack wrote On 02/14/06 14:55,:
Hi all,

I've been designing a program which would need to have a function to
replace values in a string.
For example I could have a string:
"<circle x="%" y="%">" and then I would replace the % with actual

[snip]

Thanks

Feb 14 '06 #4
Ben Pfaff <bl*@cs.stanford.edu> writes:
"gamehack" <ga******@gmail.com> writes:

[...]
I'm thinking of writing a function like: char* get_circle(int
x, int y) {
}

But that would return a char* and not the actual string.


I'm puzzled how a char *, that presumably points to the start of
a string, is not an actual string.


Um, because a string is an array, and a char* is a pointer (which
might point to the start of a string). If a C function could return
an actual string, we wouldn't have to worry about memory allocation.
I'm certain you know that; I'm puzzled by your puzzlement.

--
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.
Feb 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1853
by: Robert | last post by:
Hello, Can anyone help with this code? I need to split a long piece of text from a textarea box into small chunks, then POST these chunks to my credit-card provider, whereupon he will POST...
6
1895
by: lkrubner | last post by:
Last year I asked a bunch of questions about character encoding on this newsgroup. All the answers came down to using ord() in creative ways to try to make guesses about multi-byte characters. I...
0
1155
by: ani | last post by:
I have a questionaire page , which basically has questions with multiple choice answers. I need to accomplish paging on this and there are few questions that are gender specific. According the...
6
11475
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
7
1449
by: millerm | last post by:
I'm obviously new to C, and have been trying different things to get this done, but I'm at the end of the line and need some suggestions. I am reading a string in from a user, in the form of a...
14
2436
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
6
2002
by: Gidi | last post by:
Hi, I'm writing a C# Windows Application and i have 3 questions: 1. I have a string which i want to write into file but i want to write it in ASCII format, how can i do it? 2. I need my...
5
2229
by: Mart | last post by:
Hi everybody, I plan to have a LOT of SQL string in my app. So I need your advice for this. Is it a good idea to store all my SQL string in the app.config file? For a maintenance point of...
4
1866
by: Harro de Jong | last post by:
(absolute beginner here, sorry if this seems basic) Section 7.10 of 'How to Think Like a Computer Scientist' contains this discussion of string.find and other string functions: (quote) We can...
28
301
by: pradeep | last post by:
Hello friends: I know some people here don't like to answer C++ questions, but I believe this is really about the underlying C code. Anyway I have posted as well to the other group someone...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7229
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...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.