Hello,
I wanna illustrate the difference between a char array (a string that is) and a pointer to char using a simple function which say capitalizes all the letters of a given string.
To that affect I have written a simple code which (hopefully) should be doing just that. I have a function which does the checkin g and the capitalizing part. The function accepts a char pointer as an argument so I pass it a char pointer once and a char array the other time.
So I hope that the former one should give me a seg fault as i am trying to access and write back to a memory element which does not belong to the function (is it a scope issue or what??...im bedazled as of now). And yeah it does flag a seg fault for the former case.
However when I pass the array (using the name of the array which is also a char pointer) it still flags the seg fault error!!!..why is that..
heres the code:
Code:#include <stdio.h> #include <ctype.h> void set2caps(char *);//func prototype for capitalizing letters of string int main() { char *str = "string for pointer"; // char str1[] = "string for array"; set2caps(str); set2caps(str1); puts("strings displayed in caps\n"); // printf("str in caps is %s\n",str); printf("str1 in caps is %s\n",str); return 0; } void set2caps(char *str_hld) { unsigned int chr_cnt = 0; char temp; while(str_hld[chr_cnt]) { if (!isupper(str_hld[chr_cnt])) temp = toupper(str_hld[chr_cnt]); str_hld[chr_cnt] = temp; chr_cnt++; } }



LinkBack URL
About LinkBacks




