As for the first blue part, "%p" returns the address and it does return the address successfully.
Okay, so you're getting lucky. How long do you think you will continue to be lucky?
From some documentation for printf() since it is referenced above:
p
The void * pointer argument is printed in hexadecimal (as if by %#x or %#lx).
So notice that printf() is expecting a void*, not a char*. The reason you are probably being lucky is because you're dealing with a char pointer.
For the second blue part, how are there some software applications that do not limit ur input or am I having a wrong perspective.
I'm not talking about "software applications", I'm talking about functions/methods. In this case scanf(). When processing a string scanf() will continue processing the string until it encounters some whitespace character no matter how big the the array actually is. So to eliminate the problem, always use a method that stops the processing before overflowing the bounds of the array.
The scanf() function uses the optional width format specifier as the limit:
Code:
char my_strting[10];
scanf("%9s", my_string); // Notice the width specifier, and see below.
Notice that the width specifier is one less than the actual size of the array, this is because you must reserve room for the end of string character that will be added to the string by scanf().
I didn't understand the fourth part, do you mean the pointer temp or the function itself, thats my only question.
In this case I meant the pointer temp, that is local to that function. You can return a pointer from the function but it must be either a pointer that has been declared as static, a dynamically allocated pointer, or you can return a pointer that is a parameter.
I am only writing this simple code to understand the difference between arrays and pointers and how strings are just pointers.
Don't get yourself confused by thinking strings are "just pointers", they are not "just pointers", nor are they just arrays. In C a string is an array of char that is terminated by the end of string character ('\0'). Without this terminator you just have an array of char. Also an array is subtly different than a pointer, but an array decays very easily into a pointer to the first element of the array.
Be careful with that "array" you are passing into that function. It is actually a pointer to a constant array of char.
That function should really be defined as:
Code:
char *get_string(const char *s);
// But IMO the following would be better:
char *get_string(const char s[]);
Both the above declarations would be treated the same by the compiler, but IMO the second strongly hints that you're passing a string not a pointer to a single character.
By the way your original code gives these messages when I try to compile it:
main.c||In function ‘get_string’:|
main.c|11|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat=]|
If you're not getting any warnings you should see about increasing your compiler warning levels.
Remember that the name of the array is a pointer so you don't need the ampersand.
When you fix that issue you should then get this warning:
main.c||In function ‘get_string’:|
main.c|14|warning: ‘temp’ is used uninitialized in this function [-Wuninitialized]|
Now making temp a local array:
Code:
char temp[100];
scanf("%99s", temp);
Produces this error:
main.c||In function ‘get_string’:
main.c|17|warning: function returns address of local variable [-Wreturn-local-addr]|
Now one way to fix this problem:
Code:
char *get_string(const char *s)
{
printf("%s\n", s);
static char temp[100];
scanf("%99s", temp);
return temp;
}
But be careful if you call the function multiple times the values pointed to will change.