On Wed, 11 Jun 2008, Lim Hock-Chai wrote:

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?

No, it absolutely does not. A pointer is simply that: a pointer to a place in memory. The string "assign a value" exists in memory somwhere and you are simply assigning the value of the pointer s1 to be the location of the first memory cell that contains the start of the string. The string exists independantly of the pointer.

char * s1
int myInt = 10;

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

Because C does not allocate storage automatically. In this code snippet you done the following:

1. char * s1; - this simply allocates storage to hold a variable of type pointer to char. The allocated storage hasn't been set to anything and is only enough storage to hold a pointer to a char - nothing more.

2. int myInt = 10; - here you have allocated storage to hold a variable of type int and initialized that storage to contain the value 10;

3. sprintf(s1, "test %i", myInt); - this has serious problems. What you're doing here is basically writing to a buffer that doesn't exist. Let's look at some relevant parts from the sprintf() man page:

int sprintf(char *str, const char *format, ...);

The functions in the printf() family produce output according to a for-
mat as described below. The functions printf() and vprintf() write
output to stdout, the standard output stream; fprintf() and vfprintf()
write output to the given output stream; sprintf(), snprintf(),
vsprintf() and vsnprintf() write to the character string str.

From that last sentence we see that sprintf() writes to a buffer pointed
to by the pointer variable in the first argument. But in your case you have only allocated the storage to hold a variable pointer by declaring it, it doesn't actually point anywhere yet. You need to use malloc() to allocate a buffer that sprintf() can write to:

s1 = malloc (sizeofbuffer * sizeof(char));

Of course now you have to be careful that sprintf() doesn't try to print a string that is longer than the buffer size you have created (sizeofbuffer in the above example). The solution to this nasty problem is to use snprintf() instead:

snprintf (s1, sizeofbuffer - 1, "test %i", myInt);

This will prevent the buffer overflow problem.

James Rich

It's not the software that's free; it's you.
- billyskank on Groklaw

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

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

This mailing list archive is Copyright 1997-2022 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.