× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



The three line example you presented would work, but beware, you only allocated 20 bytes and you have more than that in your message. C will let you get yourself into a lot of trouble with simple mistakes like that.

You will need to "manually" allocate a buffer somehow, as you have done, no little cute shortcuts like your one-line example, like you could do in Java or other languages.

If you knew your message would always be built from a string and an int, you could make your function accept those arguments like

Void myFunction(char *message, int myInt)

and then build your message within myFunction, but I don't think that's what you were looking for.

-Marty

-----Original Message-----

From: Lim Hock-Chai
Sent: Thursday, June 12, 2008 10:59 AM
To: C programming iSeries / AS400
Subject: Re: [C400-L] basic c question about char *


Thank you for all the reply. Now I've a better understanding of char *.


One more question:

Void myFunction(char *message)

myFunction("my static message");

// above function call is pretty straight forward. But what if I need
to pass non static message to it? Is there a way to do it without
having to create a char * or char [] variable. Something like this (I
know the syntax is not correct):

myFunction("my static message" + myInt);


Or do I have to do

char myMessage[20];
sprintf(myMessage, "my non Static message %i", myInt);
myFunction(myMessage);




-----Original Message-----
From: c400-l-bounces+lim.hock-chai=usamobility.com@xxxxxxxxxxxx
[mailto:c400-l-bounces+lim.hock-chai=usamobility.com@xxxxxxxxxxxx] On
Behalf Of Lim Hock-Chai
Sent: Wednesday, June 11, 2008 4:36 PM
To: C programming iSeries / AS400
Subject: [C400-L] basic c question about char *

can somebody help me understand how char * work?

char * s1;
s1 = "assign a value";
s1 = "assign another value"; //does c auto deallocate storage and
reallocate a longer one for this assignment?

. . .



char * s1
int myInt = 10;

sprintf(s1, "test %i", myInt); //why is s1 contains *null after this
assignment?




--
This is the C programming iSeries / AS400 (C400-L) mailing list To post
a message email: C400-L@xxxxxxxxxxxx To subscribe, unsubscribe, or
change list options,
visit: http://lists.midrange.com/mailman/listinfo/c400-l
or email: C400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives at
http://archive.midrange.com/c400-l.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.