What are the potential problems of passing strings directly to a function where input parameter is char pointer in C? -


in below pusedo code, function display has char* input parameter , string "apple" passed. in function elements of string accessed index. here there no memory string "apple" how getting accessed in display function

#include <stdio.h> void display(char * str) {    int = 0;    while ('\0' != str[i])   {    printf("%c", str[i]);    i++;   } }  int main() {    display("apple");    return 0; } 

the function works correctly , gives output apple. approach seen in many programs know memory assigned string "apple". , potential problems in usage.

here there no memory string "apple" how getting accessed in display function

of course there memory string "apple": string literals placed in memory, , literal serves pointer memory. exact location implementation-defined, common place string literals in memory segment occupied executable code of program.

what potential problems in usage?

none. long not try assigning memory of string literal, safe.


Comments