char* str1="string";
This makes str1 pointer; it points to the first character of a string literal. You must define it as const , because you are not allowed to modify the string literal:
const char *str1 = "string";
...
char str2[7]="string";
This makes str2 a char array (not a pointer) and copies the contents of the string literal into it. There is no need to define it as const ; the array itself is writable. You can also omit the size and define it as an initializer:
char str2[] = "string";
Then sizeof str2 == 7 (6 bytes for "string" plus 1 for the final '\0' ).
It:
char* str3=(char)malloc(sizeof(char)*7);
It is written incorrectly, and it does not even compile; at least you should get a warning from your compiler. You type the result of malloc() to enter char . You should convert it to char* :
char *str3 = (char*)malloc(sizeof(char) * 7);
But the throw is not needed and can mask errors in some cases; see question 7.7 and follow in the comp.lang.c FAQ :
char *str3 = malloc(sizeof(char) * 7);
But sizeof(char) is 1 by definition, so you can simply write:
char *str3 = malloc(7);
malloc() allocates memory, but it does not initialize it, so if you try to print the line pointed to by str3 , you will get garbage - or even crash at runtime if the allocated space does not contain the terminating null character '\0' . You can initialize it with strcpy() , for example:
char *str3 = malloc(7); if (str3 == NULL) { fprintf(stderr, "malloc failed\n"); exit(EXIT_FAILURE); } strcpy(str3, "string");
You must be very careful that the data you copy is no more than the allocated space. (No, `strncpy () is not the answer to this problem .)
void main() invalid; it should be int main(void) . If your tutorial told you to use void main() find the best tutorial; its author does not know C. very well.
And you need the appropriate #include directives for any library functions that you use: <stdio.h> for printf() , <stdlib.h> for exit() and malloc() and <string.h> for strcpy() . The documentation for each function should indicate which title should include.
I know that it is a lot to devour; don't expect to understand everything right away.
I mentioned the comp.lang.c FAQ ; this is a great resource, particularly section 6, which discusses arrays and pointers and the often confusing relationships between them.
As for your question 3, how to return a string from a C function, which turns out to be surprisingly complicated because C makes memory allocation (basically it leaves to manage it yourself). You cannot safely return a pointer to a local variable because the variable ceases to exist when the function returns, leaving the caller with a dangling pointer, so returning your str2 is dangerous. The return of the string literal is fine, as this corresponds to an anonymous array that exists for the entire execution of your program. You can declare an array using static and return a pointer to it, or you can use malloc() (this is the most flexible approach, but that means the caller needs free() memory), or you can require the caller to pass a pointer to the buffer where your function will copy the result.
Some languages ββallow you to build a string value and simply return it from a function. C, as you open now, is not one of these languages.