arrays of pointers to strings

Started by
12 comments, last by popcorn 20 years ago
Sorry I should point out that I am programming in C only.

I tried the program in your post Oluseyi but it just crashed. For me the program looks like:

#include <stdio.h>
#include <stdlib.h>

char *str1 = "Monday";
char *str2 = "Monday";

int main(int argc, char *argv[])
{
strcpy(str1, "Fri");
printf("str2: %s\n", str2);

system("PAUSE");
return 0;
}

Just to make things clear for myself I take it that its possible to create a statement like this:

char *pString = "hello";

and then do this

strcpy(pString, "hel"); because this will still write to a valid area of memory.

But doing something like this

strcpy(pString, "helloworld"); will not work since you are trying to write to memory that you should not have access to.



[edited by - popcorn on April 10, 2004 10:14:17 PM]
How about them apples?
Advertisement
quote:Original post by popcorn
I tried the program in your post Oluseyi but it just crashed.
Compilers and operating systems have gotten smarter! It "crashed" for me, too; apparently the application, trying to overwrite the data segment of the executable (which is fixed, silly me) is, in effect, trying to overwrite itself - but executables are locked while in use.

But yeah, that''s what used to happen.
HI,
Use %s instead of %c in the printf argument.
thank you.

----------------->ASH

[edited by - ash1894 on April 11, 2004 6:10:46 AM]
Thanks for the explanation Oluseyi.
How about them apples?

This topic is closed to new replies.

Advertisement