This question already has answers here:
Returning an array using C
(8 answers)
Closed 2 years ago.
I have a code and can't figure out why it's crashing (segmentation fault) I know from past posts that it has something to do with unaccessable memory (I think), but I initialized my "input" variable.
#include <stdio.h>
#include <stdlib.h>
char *getInfo() {
char input[1000];
scanf("%s", input);
return input;
}
int main() {
char *x;
x = getInfo();
printf("%s\n", x);
return 0;
}
When I run and backtrace the program inside gdb, it says (among other things) "... in main () at error.c:11"
When I break at line 11 after giving input ("bark") try to print the variables, print input gives me '\000 x29' and print x gives me 0x0. I know that 0x0 means that it's null, and I think \000 also means null, but I don't get why., when I scaf'ed input, shouldn't the null be replaced?
Your function getInfo returns the address of the local variable input. But this variable is deleted when the function returns, so the pointer is becoming invalid. The regular way to solve this is by passing the array into the function as a parameter.
char *getInfo(char *input) {
scanf("%s", input);
return input;
}
and on the caller side:
char input[1000];
char *x = getInfo(input);
Of course in your case I would just move the call to scanf into main:
char input[1000];
scanf("%s", input);
printf("%s\n", input);
Please also note that your call to scanf is not safe as scanf might read more input than the buffer can hold. Also my very simple change for getInfo has the disadvantage that you do not pass the length. Normally you should pass the pointer to the buffer and the size of the buffer. That way you can make sure that the function does not overflow the buffer.
For safer variants in your case you might also consider one of these lines:
scanf("%999s", input);
fgets(input, 1000, stdin);
Related
This question already has answers here:
Scanf fails with bus error
(2 answers)
Closed 6 months ago.
below is my code
#include <stdio.h>
#include <string.h>
char* get_string(char* question);
int main(void)
{
char* name = get_string("Enter a name:");
printf("%s\n", name);
return 0;
}
char* get_string(char* question)
{
printf("%s", question);
char* input;
scanf("%s", input);
return input;
}
It compiles just fine without any warning or errors, but when I run the code I got this
Bus error: 10
In your function get_string() you try to take input using scanf() using a uninitialized pointer called input.
Since input points to nothing, reading into it causes undefined behaviour.
To fix it you should allocate memory for your string:
char *input = malloc(sizeof(char) * input_size);
Also don't forget to free() your input buffer when you are done using it:
free(input);
I have a char pointer: char *sentences; and I read it like this:
#include <stdio.h>
int main() {
char *sentences;
sentences="We test coders. Give us a try?";
printf("%s", sentences);
return 0;
}
but I want to read with scanf() function in c.
scanf("%[^\n]s",S); or scanf("%s",S); didn't work.
How can I do that?
Are you declaring the variable char *sentences; and immediately trying to write to it with scanf? That's not going to work.
Unless a char * pointer is pointing to an existing string, or to memory allocated with malloc-family functions, assigning to it with scanf or similar is undefined behavior:
char *sentences;
scanf("%s", sentences); // sentences is a dangling pointer - UB
Since you haven't actually shared your code that uses scanf and doesn't work, I can only assume that's the problem.
If you want to assign a user-supplied value to a string, what you can do is declare it as an array of fixed length and then read it with a suitable input function. scanf will work if used correctly, but fgets is simpler:
char sentence[200];
fgets(sentence, 200, stdin);
// (User inputs "We test coders. Give us a try")
printf("%s", sentence);
// Output: We test coders. Give us a try.
Also, never, ever use gets.
This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 4 years ago.
Here is a simple program that is a function which checks for the character 'a' within a string, then returns the character if found, and NULL if it is not found. I am not really sure if it is the function or the call of the function itself, here is the code.
#include <stdio.h>
char *find_char(char *str, char character);
int main(){
char *str;
printf("str\n");
scanf("%s", str);
printf("%c",*find_char(str,'a'));
return 0;
}
char *find_char(char *str, char character){
char *pstr = str;
while(*pstr!='\0' && *pstr!=character){
pstr++;}
if (*pstr!=character)
return NULL;
else
return pstr;
}
Your problem basically lies in these two lines, the first and third code line of your main function:
char *str; // Create pointer, pointing to ***arbitrary*** memory.
scanf("%s", str); // Write to that memory, undefined behaviour.
You need to create backing storage for the pointer so you have somewhere valid to write your input to.
A better idea would be to use a rock-solid input routine rather than relying on often-dodgy practices like writing to invalid memory, or allowing uncontrolled input into limited-size buffers. One such beast can be found here.
This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
Closed 7 years ago.
I'm trying to make a function which will receive a char * from the user and will print it.
It turns my value to something weird when I'm printing it.
**//input method**
char* readContactName(){
char tmp[20];
do{
printf("What is your contact name?: (max %d chars) ", MAX_LENGH);
fflush(stdin);
scanf("%s", &tmp);
} while (!strcmp(tmp, ""));
return tmp;
}
void readContact (Contact* contact)
{
char* tmp;
tmp = readContactName();
updateContactName(contact, tmp);
}
**//when entering this function the string is correct**
void updateContactName(Contact* contact, char str[MAX_LENGH])
{
printf("contact name is %s\n",&str); --> prints rubish
}
What did I miss here?
In your code, char tmp[20]; is local to the function readContactName(). Once the function finishes execution, there is no existence of tmp. So, the address-of-tmp also becomes invalid.
So, after returning, in the caller, if you try to use the returned pointer, (as you're doing in updateContactName(contact, tmp);()) it will invoke undefined behaviour.
FWIW, fflush(stdin); is also UB. fflush() is only defined for output streams.
Solution:
Define tmp as to be a pointer.
Allocate memory dynamically (using malloc() or family).
Once you're done using the allocated memory, you need to free() it also.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
why does c allow initialization of string without declaration?
I am trying this rather simple code:
#include<stdio.h>
void copy(char *,char *);
main() {
char *name;
char *surname;
printf("Enter name: ");
scanf("%s",name);
printf("%s",name);
}
But for some reason, after I enter a name and press Enter, the program hangs and gives a message like Program has stopped working. However when I remove the 2nd character pointer declaration, that is the char *surname;, it works just as expected. What is the problem here?
You have not allocated memory for the pointers, so the scanf accesses arbitrary unspecified memory, which is undefined behaviour.
You need to pass pointers to sufficiently large memory blocks to scanf, either declare
char s1[100], s2[100];
(if 100 is large enough), or malloc memory
char *s1 = malloc(100);
char *s2 = malloc(100);
if (!s1 || !s2) {
// malloc failure
exit(EXIT_FAILURE);
}
You are writting into unallocated memory. That is undefined behavior,
You can do 2 things here:
declare your arrays of chars as having a fixed size at compile-time like this: char name[100]; (which means you can't change their size at runtime)
allocate room for char *name using malloc() or calloc() functions in stdlib.h
In any case you absolutely have to make sure you only allow the user to input the amount of bytes you allocated, otherwise bad things can and will happen!
A small study on what an evil person can (and will ;) do if you fail to define said boundaries can be found here: http://www.cultdeadcow.com/cDc_files/cDc-351/
Because you didn't allocate memory for it, and the string you put in it screws the code of the program. Try to use sscanf and getline:
#include <stdio.h>
int main()
{
int nbytes = 100;
char *my_string;
int int1, int2, int3;
int args_assigned;
args_assigned = 0;
while (args_assigned != 3)
{
puts ("Please enter three integers separated by whitespace.");
my_string = (char *) malloc (nbytes + 1);
getline (&my_string, &nbytes, stdin);
args_assigned = sscanf (my_string, "%d %d %d", &int1, &int2, &int3);
if (args_assigned != 3)
puts ("\nInput invalid!");
}
printf ("\nThanks!\n%d\n%d\n%d\n", int1, int2, int3);
return 0;
}
check out this:
Reading in a variable length string user input in C
and this :
http://en.wikipedia.org/wiki/Buffer_overflow
You declare a pointer and do not give it a valid memory address, it points to a random addrss. You cannot read or write with this pointer. Pointers should be used like this:
char s1[100],s2[100];
char * name=s1;
char * surname=s2;