What's wrong with this scanf()? - c

Am I using scanf() in some wrong way?
char *input;
scanf("%s", input);
printf("%s\n", input);
This fails at the run-time.

Declaring a char * only creates a pointer, it does not allocate any memory for the string. You need to allocate memory for input. You can do that dynamically via malloc (and free when done) or you can declare an array of static size like char input[100].

char *input;
This is a pointer. It doesn't point to any memory.
#include <stdlib.h>
#include <stdio.h>
int main()
{
//char *input;
char input[128];
memset(input, 0 ,sizeof(input));
scanf("%s", input);
printf("%s\n", input);
return 0;
}

replace char *input; with char input[1024] = {0};
you should ensure the parameter you pass to scanf points to a buffer which could hold your input

Related

Difference between scanf for char * and char [ ]?

I'm a newbie to c programming. I'm trying to input two strings using scanf. My first try was as below
#include <stdio.h>
int main(void)
{
char *word1;
char *word2;
scanf("%s", word1);
scanf("%s", word2);
printf("%s\n", word1);
printf("%s\n", word2);
}
If I run this code, only the first input is correctly stored (word2 is null). But, if I run the code below, both inputs are correctly stored in word1 and word2.
#include <stdio.h>
int main(void)
{
char word1[10];
char word2[10];
scanf("%9s", word1);
scanf("%9s", word2);
printf("%s\n", word1);
printf("%s\n", word2);
}
What is the problem with using pointers with scanf?
There is no problem in with using a pointer as your scanf argument in principle. In your specific case, you've simply not initialized those pointers, so you're causing undefined behaviour. Modifying your second (correct) example to use pointers:
#include <stdio.h>
int main(void)
{
char word1[10];
char *p1 = word1;
char word2[10];
char *p2 = word2;
scanf("%9s", p1);
scanf("%9s", p2);
printf("%s\n", p1);
printf("%s\n", p2);
}
The important thing to remember about pointers is that they have to point to something in order to be useful.
What is the problem with using pointers with scanf?
The first one is undefined behaviour. Look at this part of code:
char *word1;
char *word2;
scanf("%s", word1);
scanf("%s", word2);
No memory allocated to word1 and word2 pointers. When you give input, scanf() end up accessing unallocated pointers and dereferencing an unallocated/invalid pointer is undefined behaviour.
You should make sure that before using/accessing a pointer, it should be pointing to a valid memory location. You can either make it point to an exiting valid memory or allocate memory dynamically to it, like this:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *word1;
char *word2;
word1 = malloc (10);
if (word1 == NULL) {
exit (EXIT_FAILURE); // or whatever you want to do to handle memory allocation failure
}
word2 = malloc (10);
if (word2 == NULL) {
exit (EXIT_FAILURE); // or whatever you want to do to handle memory allocation failure
}
scanf("%9s", word1);
scanf("%9s", word2);
printf("%s\n", word1);
printf("%s\n", word2);
// once done with allocated memory, free it
free (word1);
free (word2);
return 0;
}

How do I use a string variable in the printf() function in c?

I am running the following c program:
#include <stdio.h>
int main() {
char str1;
printf("What is your name? ");
scanf("%s.", str1);
printf("Hi there %s.", str1);
return 0;
}
But this what it returns:
What is your name? Varun
Hi there (null).
Why does it say (null)? Please answer.
You have multiple mistakes here:
You use %s for a char variable.
You're not passing a pointer to scanf function. You're just passing a garbage value.
You introduced an undefined behavior. And you should have some compile-warnings that tells you that you have mistakes.
char str1; you have space for only 1 char , that's not much , try replacing with char str1[30]; and try again.
Try this instead:
#include <stdio.h>
int main() {
char str1[1000]; // <-- here are the changes
printf("What is your name? ");
scanf("%s.", str1);
printf("Hi there %s.", str1);
return 0;
}
To use strings in c, you need to create a char array with a specific size
char arr[1000]; // array of size 1000, it can contain a string of 1000 letters
then to fetch the string you have to use
scanf("%s", arr);
then to print it back use
printf("%s", arr);
#include <stdio.h>
#include <malloc.h>
int main() {
char *str;
str = (char *)malloc(sizeof(char) * 10);
printf("What is your name?");
scanf("%s", str);
printf("Hi there %s.", str);
return 0;
}
I think you should do like this, you should understand char and string.

Passing argument 1 of 'fgets' makes pointer from integer without a cast

I run this piece of code and i get the following error, i did some research but i didn't really got an answer i hope if someone of you could give me a little help. I am new to programming please spare me if i missed something too obvious.
[Warning] passing argument 1 of 'fgets' makes pointer from integer without a cast
int main(int argc, char *argv[]) {
char *t,*s;
char first, second;
int x;
printf("Give the first string: ");
fgets(first,sizeof(char),stdin);
printf("Give the second string: ");
fgets(second,sizeof(char),stdin);
}
When i add a "&" inside my "first" and "second" variables it compiles but when i run it i don't get the strings that i gave from the keyboard.
How to make it compile?
fgets expects char* pointing to a buffer as it's first argument
int main(int argc, char *argv[]) {
char first[80], second[80];
printf("Give the first string: ");
fgets(first, sizeof(char), stdin);
printf("Give the second string: ");
fgets(second, sizeof(char), stdin);
}
fgets means "get a string from a file", and you need an array of char to hold a string.
you should turn first and second into pointers and allocated space to fill them in
int main(int argc, char *argv[]) {
int flen = 256;
int slen = 256;
char* first = malloc(flen); //allocate enough space to handle verbose typists :)
char* second = malloc(slen);
int x;
printf("%s\n","Give the first string: ");
fgets(first,flen,stdin);
printf("%s\n","Give the second string: ");
fgets(second,slen,stdin);
//do something with first and second.
//free memory you allocated
free(first);
free(second);
};

_strrev function isnt working inside a function, but works outside

I’m trying to get the _strrev function to work but when I put my string into a function it doesn’t seem to work, just when I'm out of the function..
I’m getting so frustrated because I'm not getting anywhere with this..
Here's my code so far
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void reverse(char *string) {
char *str, temp;
int begin = 0, end = 0;
char word[64];
int jaja = 0;
printf("Your string from the function is %s\n", string);
printf("%s\n", _strrev(&string)); //And why isnt this working
jaja = strlen(string);
printf("Your string has %d characters\n", jaja);
}
int main()
{
char *str;
scanf("%s", &str);
printf("%s\n", _strrev(&str)); //How come this works
reverse("Okay");
getchar();
return(0);
}
So I would love some guidance where my mistake is, I seriously cant find it.
According to MSDN The prototype for _strrev is
char *_strrev(
char *str
);
If you have a char *string you must call it like this :
printf("%s\n", _strrev(string));
In this case
printf("%s\n", _strrev(&string));
you are passing a char**
To answer the question you posted in your code comments:
int main()
{
char *str;
scanf("%s", &str);
printf("%s\n", _strrev(&str)); //How come this works
That works because you're lucky, as your call to scanf() places whatever it reads into the actual memory used for the pointer str, and you're not entering enough data to cause problems. Try entering a really long string when running this program and it won't work as well.
You need to actually have a char buffer to read data into, like this:
int main()
{
char str[256];
scanf("%s", str);
printf("%s\n", _strrev(str));
or
int main()
{
char *str = malloc( 256 );
scanf("%s", str);
printf("%s\n", _strrev(str));
And as pointed out in the comments to the question, you can still overrun your buffer.

segmentation-error in ubuntu gcc

#include<stdio.h>
int main()
{
char *ch;
int n=10;
gets(ch);
puts(ch);
printf("%d\n",n);
}
#include<stdio.h>
int main()
{
char *ch;
int n=10;
gets(ch);
printf("%d\n",n);
puts(ch);
}
In the first one , the segmentation error occurs at print(n) and in second one it occurs at puts(ch).No error occurs if print(n) is also used just after declaring n.
gets() is dereferencing an unitialized pointer, causing undefined behaviour.
Allocate memory for ch and don't use gets() as there is no way to limit the number of characters read, meaning potentially writing beyond the bounds of the destination array.
Example using fgets():
char ch[128];
if (fgets(ch, 128, stdin))
{
}
Use fgets and allocate memory for your "buffer" (via malloc) to hold the given string. At the end call free for your pointer.
#include<stdio.h>
#include<stdlib.h>
int main(){
char * ch = (char*) malloc(sizeof(char)*10);
//or by using this: char ch[10];
int n=10;
gets(ch);
puts(ch);
printf("%d\n", n);
free(ch);
}

Resources