kahroba92
Junior Member level 1
Hi freinds,
I want to write five 'M' in a string. I have defined string by char * as following code:
if I write "char str[5]" instead of "char *str" my code will execute well. I can't understand why this pointer to string doesn't work.
what's my problem? can anybody help?
tnx
I want to write five 'M' in a string. I have defined string by char * as following code:
Code:
#include [COLOR="#000000"]<[/COLOR]stdio.h>
#include [COLOR="#000000"]<[/COLOR]stdlib.h>
int main()
{
char *str;
int i=0;
while(i<5)
{
*(str+i) = 'M';
i++;
}
*(str+5) = '\0';
printf("%s", str);
return 0;
}
if I write "char str[5]" instead of "char *str" my code will execute well. I can't understand why this pointer to string doesn't work.
what's my problem? can anybody help?
tnx
Last edited by a moderator: