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

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);
};

Related

_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.

Code for extracting string crashes

I wrote this code to accept a string and until where it should extract a string and print it.
Given below is the code:
#include <stdio.h>
#include <stdlib.h>
int strlen(char s[]){
int i = 0;
while(s[i]!='\0')
i++;
return i;
}
char *extract(char s[], int n){
char *result = (char *)malloc(sizeof(char)*3);
for(int j=0;j<n;j++){
result[j]=s[j];
}
return result;
}
int main(){
char str[100];
int till;
printf("Enter a string: ");
scanf("%s", str);
printf("Until where to extract: ");
scanf("%d", till);
char *ret = extract(str, till);
printf("%s is extracted", ret);
return 0;
}
This is what happens:
Enter a string: hello
Enter from where to extract: 2
And then it crashes.
I don't see what the problem is.
At very first, you need to change
scanf("%d", till);
to
scanf("%d", &till);
as scanf() needs a pointer to the data type argument for the supplied format specifier. Using a wrong type of argument causes undefined behavior.
Then, there are many issues, like
You're allocating only 3 chars, where you're looping based on the incoming value of n.
You never checked for the success of malloc().
You're not null-terminating your result which you intend to use as a string later.
That said,
You should always limit the input for your strings to avoid the possibility of overrun, like
scanf("%99s", str); //considering the length of the array is 100.
There is a library function strlen() available with string.h. Even if you want to roll out your own functions, try to follow a different naming convention.
You did not free() the allocated memory.
I you add -Wall to command when you compile your code you'll see
test.c:40:2: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d", till);
^
Then change you must change it to scanf("%d", &till);
In addition to the other suggestions:
char *result = (char *)malloc(sizeof(char)*3);
Should be:
char *result = malloc(n + 1);

array changes when i change parameter

So i have this code in c.It all works fine until i get to the point to read word again.It gets the new word but also the (*A)[size-1] takes the price of the new word.How do i prevent this?
void fuction(char ***A,char ***B,int size)
{
char word[20],word2[20];
printf("Type word .\n");
gets(word);
while(strcmp(word,"0")!=0)
{
printf("Type second word.\n");
gets(word2);
printf("%d",size);
**A=realloc(**A,(size+1)*sizeof(char));
**B=realloc(**B,(size+1)*sizeof(char));
(*A)[size-1]=word;
(*B)[size-1]=word2;
size++;
printf("Type another word to add or 0 to exit.\n");//**it all works fine**
gets(word);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void function(char ***A, char ***B, int *size){
char word[32], word2[32];
printf("Type first word.\n");
scanf("%31s", word);
while(strcmp(word,"0")!=0){
printf("Type second word.\n");
scanf("%31s", word2);
*A =realloc(*A, (*size+1)*sizeof(char*));
*B =realloc(*B, (*size+1)*sizeof(char*));
(*A)[*size]=strdup(word);
(*B)[*size]=strdup(word2);
++*size;
printf("Type another word to add or 0 to exit.\n");
scanf("%31s", word);
}
}
int main(void){
int i, size = 0;
char **w1, **w2;
w1 = w2 = NULL;
function(&w1, &w2, &size);
for(i = 0; i < size; ++i){
printf("%s, %s\n", w1[i], w2[i]);
free(w1[i]);free(w2[i]);
}
free(w1);free(w2);
return 0;
}
Turns out the problem was that i didnt allocate memory for the words in the array.I added these lines and it worked.Thank you for your answers.
(*A)[size-1]=(char*) malloc(31);
(*B)[size-1]=(char*) malloc(31);
This
(*A)[size-1]=word;
(*B)[size-1]=word2;
is not what you think it is.
In c, this means you are assigning the address to the first element of the array word to (*A)[size-1] if you want this to work, provided that you have allocated memory for (*A)[size-1] you should do it this way
strcpy((*A)[size-1], word);
strcpy((*B)[size-1], word2);
You should think about why do you need char ***, generally you wont need more than char **, and don't use gets() use fgets() instead.

What's wrong with this scanf()?

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

How to create a string-type variable in C

Question
How to declare a string variable in C?
Background
In my quest to learn the basics of c, I am trying to port one of my oldest python programs, Bob, to C. In the program, the script asks the user for information on him or herself, and then spits out responses. Almost all of these variables use raw_input for their information - the variables are strings. But, I have found no way to declare C variables.
Code
So far, I have tried to declare the variable as of type char and int. Here is the code, switch the type at your leisure.
int main(int argc, const char * argv[])
{
int name;
printf("What is your name?");
scanf("%s",&name);
printf("Your name is %s", name );
return 0;
}
Error Message
When I run this code, Xcode returns some weird stuff. This part of the globidty-gloop is highlighted.
0x7fff96d2b4f0: pcmpeqb(%rdi), %xmm0
Lasty, this Yahoo Answer said that I had to use something called a character array. It was posted 5 years ago, so I assumed that there was a better way.
EDIT
I am following the tutorial at C Programming.
char name[60];
scanf("%s", name);
Edit: restricted input length to 59 characters (plus terminating 0):
char name[60];
scanf("%59s", name);
The int your putting is not a string, a string looks like "char myString[20]".
Not like "int name", that's an integer and not a string or char. This is the code you want:
int main(int argc, const char * argv[])
{
char name[9999];
printf("What is your name?\n");
scanf("%s", name);
system("cls");
printf("Your name is %s", name);
return 0;
}
In C you can not direct declare a string variable like Java and other language. you'll have to use character array or pointer for declaring strings.
char a[50];
printf("Enter your string");
gets(a);
OR
char *a;
printf("Enter your string here");
gets(a);
OR
char a[60];
scanf("%59s",a);
TESTED ON XCODE
You can do so:
int main(int argc, const char * argv[])
{
int i;
char name[60]; //array, every cell contains a character
//But here initialize your array
printf("What is your name?\n");
fgets(name, sizeof(name), stdin);
printf("Your name is %s", name );
return 0;
}
Initialize the array, is good to avoid bug
for(i=0;i<60;i++){
name[i]='\0'; //null
}
Instead int is used for int number (1, 2, 3, ecc.); For floating point number instead you have to use float
C does not have a string variable type. Strings can be stored as character arrays (char variable type). The most basic example I would add up to the rest is:
int main()
{
char name[] = "Hello World!";
printf("%s",name);
return(0);
}
It's easy!
Just put this line below, atop of your main() function.
typedef string char*;
This allows you to create a string variable as you do with integers or characters in C. After that, your program should look like this:
#include <stdio.h>
typedef char* string;
int main(void) {
string a = "Hello";
printf("%s\n", a); // %s format specifier for String
return 0;
}
For a live demonstration, visit this REPL.it.
Normally we use "&" in scanf but you shouldn't use it before variable "name" here. Because "name" is a char array. When the name of a char array is used without "[]", it means the address of the array.
replace int name; to--. char name[60];
#include <stdio.h>
int main()
{
char name[648];
printf("What is your name?");
scanf("%s", name);
printf("Your name is %s", name );
return 0;
}

Resources