-
having some problems...
I have the following code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE (sizeof(strings)/4)
int main(void)
{
int i;
char *strings[] = {
"Line1 here",
"Line2 there",
"Line3 whatever",
"Line4 blahhh",
};
for (i=0; i<SIZE; ++i)
strings[i] = strrev(strings[i]);
for (i=0; i<SIZE; ++i)
printf("%s\n",strings[i]);
system("PAUSE");
return 0;
}
I'm having problems with it, when I run it it freezes, but it compiles well, I think I'm not using pointers well.
Thanks in advance.
-
>> char *strings[]
This is an array of pointers to strings. You are initialising it to pointer to a few string literals. String literals cannot safely be modified.
Try this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE (sizeof(mystrings)/sizeof(mystrings[0]))
int main(void)
{
int i;
char mystrings[][100] = {
"Line1 here",
"Line2 there",
"Line3 whatever",
"Line4 blahhh",
};
for (i=0; i<SIZE; ++i)
strrev(mystrings[i]);
for (i=0; i<SIZE; ++i)
printf("%s\n",mystrings[i]);
system("PAUSE");
return 0;
}
-