I dont know much about C++ but i saw something in their functions:
This is an example for puts function.
Code:
* puts example : hello world! */
#include <stdio.h>
int main ()
{
char string [] = "Hello world!";
puts (string);
}
In your code you have defined str as a pointer as i can understand,whereas in the example it is not a pointer,just an aray.try using an array,not a pointer
I ussually code in Fortran and i get the error "Segmentation fault" when i have trouble with memory access,for example if i have defined an array of size 15 and i am trying to access Array[17].then i get a segmentation fault.
The problem is, it is required to strore the end of string "\0".
While printing using the 2nd method, ie using loop, you are exactly printing just the number of characters you read and printing. This will work fine. But during first method, puts(), which just prints the string by reading character by character and look for end of string, ie '\0' which is not stored in the string while reading. It is required to explicitly store "EOS" end-of-string otherwise you will have to use the first method.
No, the memory for the pointer was allocated, but it was never initialized with the address of a valid char location. In this case, the values in the memory that was allocated to it is a value that caused a segmentation error.