This question already has answers here:
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Closed 6 years ago.
I want want to read a character string, over the stdin, so I have chosen fgets. But I got this warning: initialization makes integer from pointer without a cast.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#define MAX_LINIA 301
int main(int argc,char *argv[]){
char *buffer;
printf("Enter the input\n");
if (fgets(buffer,MAX_LINIA-1,stdin)==NULL) printf("Error")
else printf("%s", buffer);
return 0:
}
You are using buffer without initialising it, which causes undefined behaviour.
You don't need to use pointer, and can use char array instead (given the max size already defined):
char buffer[MAX_LINIA];
The fgets then be better written as:
if(fgets(buffer,sizeof(buffer),stdin)==NULL)
Related
This question already has answers here:
undefined reference to `getline' in c
(3 answers)
Closed last month.
I am trying to use getline() function. I am using correct syntax I guess and also I have included #include<stdio.h> header file also. Still it is showing that [Error] 'getline' was not declared in this scope
here is my code
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *str;
int bytes_read;
int size=10;
printf("please enter a string");
str=(char *)malloc(size);
bytes_read=getline(&str,&size,stdin);
puts(str);
}
getline is a POSIX function, and Windows isn't POSIX, so it doesn't have some POSIX C functions available.
You'll need to define the feature macro _GNU_SOURCE in order to make it available for your code.
#define _GNU_SOURCE
When I run the code here happen after
scanf("%[^\n]", order);
Full code:
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#include <stdio.h>
#include <Windows.h>
#include "Colors.h"
#include <string>
#include "Functions.h"
#include <stdbool.h>
using namespace std;
int main() {
//vars
char* order = "";
int WinH;
int WinW;
bool error = false;
GetDesktopResolution(WinH, WinW);
// end vars
//funcs
//DelayedStart();
//end funcs
Sleep(1500);
system("CLS");
Sleep(250);
SetConsoleTitle(L"AI APP - Main");
printf("Hello,\nHow can I help you\n>>");
F_GREEN;
scanf("%[^\n]", order); //error here
F_B_Defalt;
if (order == "open youtube") {
ShellExecute(0, 0, L"http://www.youtube.com", 0, 0, SW_SHOW);
}
else
{
printf("%s\n", order);
puts("Whoops");
}
system("PAUSE");
}
Visual Studio 2022 V17.4
You are scanning into a string literal(""), which provokes undefined behaviour!
Actually you should have gotten a compiler warning, because of assigning this string literal, which has type char const/*!!!*/ *, to a variable of char /*non-const!*/ *. Some compilers do allow this for reasons of compatibility to C. Some of these (e.g. GCC) allow to enhance this warning to a compilation error, not sure if MSVC does so, too, but if it does, you should do so!
Additionally this string literal only contains room for one single character, so (if it was possible at all...) you only could read an empty string into (containing the terminating null character), otherwise you'll provoke undefined behaviour again for writing beyond array bounds.
Either of these two kinds of undefined behaviour might have provoked the segmentation fault.
To fix provide a writable character buffer long enough to hold expected input:
char order[128];
// ...
scanf("%127[^\n]", order);
Note how in above format string a maximum length for reading characters is specified, this needs to be, though, one less than the buffer size as the scan operation will yet append the terminating null character.
Note, too, how order decays to a pointer to its first element implicitly, so you do not need to take the address of – actually this would even be wrong and provoke undefined behaviour for providing non-matching pointer types (char(*)[128] vs. char*) to the format specifier (even though the address would be the same).
This question already has answers here:
Length of array in function argument
(9 answers)
Closed 6 years ago.
Let's provide an example. I have one main file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "otherfile.h"
char output[1024*10];
int main(){
writeSomething(output);
return 0;
}
and another file with the function:
void writeSomething(char *o){
printf("sizeof(o)=%d", sizeof(o));
//code
memset(o,0,sizeof(o));
// code
}
While compiling this, I got the warning:
argument to 'sizeof' in 'memset' call is the same expression as the
destination; did you mean to provide an explicit length?
What I want is to set the whole output array to 0 in my function, so all 1024*10 bytes. But because I pass it as a pointer, it will only set the first 8 characters to 0 (because sizeof(o)=8).
How can I memset the whole array trough a pointer? Is that even possible?
The problem is that when passed as the argument to the function, the array decays into a pointer and information about its size gets lost.
Redefine the function to
void writeSomething(char *o, size_t sz){
printf("sizeof(o)=%d", sizeof(char) * sz);
//code
memset(o, 0, sizeof(char) * sz);
// code
}
so that the call to it looks like writeSomething(output, 1024*10);.
As pointed out in the comments, the C99 standard requires that sizeof(char) be 1, so it's fine to omit the sizeof(char) in the code I provided. However, I prefer to leave it there for stylistic reasons.
This question already has answers here:
Compiler error: memset was not declared in this scope
(2 answers)
Closed 7 years ago.
I have the following c code:
#include<stdio.h>
int main(void)
{
char buff[10];
memset(buff,0,sizeof(buff));
gets(buff);
printf("\n The buffer entered is [%s]\n",buff);
return 0;
}
When I run the code, I get the following warning:
warning: implicit declaration of function ‘memset’ [-Wimplicit-function-declaration]
How should I solve the problem?
Thanks
Add
#include <string.h>
at the top of file.
This because is the header file where the memset prototype can be found by compiler.
Avoid using gets function... Use scanf or fgets instead.
Take a look HERE
Add
#include <string.h>
memset is available in string.h
So I'm new to C and I'm playing around with functions in the GNU C Library when I come across https://www.gnu.org/software/libc/manual/html_node/strfry.html#strfry
Intrigued, I wrote a little tester program:
1 #include <stdio.h>
2 #include <string.h>
3
4 main ()
5 {
6 char *str = "test123abc";
7 char *other;
8
9 other = strfry(str);
10 printf("%s\n", other);
11 return 0;
12 }
gcc test.c outputs test.c:9: warning: assignment makes pointer from integer without a cast
Why?
/usr/include/string.h has the following entry:
extern char *strfry (char *__string) __THROW __nonnull ((1));
How can a char *function(...) return int?
Thanks
Since strfry is a GNU extension, you need to #define _GNU_SOURCE to use it. If you fail to provide that #define, the declaration will not be visible and the compiler will automatically assume that the function returns int.
A related problem, as pointed out by perreal, is that it is undefined behavior to modify a literal string. Once you make the declaration of strfry visible to the compiler, this will be duly reported.
Do note that the strfry function and its cousin memfrob are not entirely serious and are rarely used in production.
To have strfry available, you need
#define _GNU_SOURCE
otherwise the prototype is not exposed and the implicit declaration is assumed to return an int.
The problem is you don't have a prototype in scope for strfry() and the compiler assumes it returns an int. When it wants to assign that int to a char* it complains with the message you specify.
According to my man pages, you need to #define _GNU_SOURCE at the very top of your source code, especially before standard #includes
#define _GNU_SOURCE
/* rest of your program */
You can't modify a literal string:
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
int main () {
char *str = "test123abc";
char other[256];
strcpy(other, str);
strfry(other);
printf("%s\n", other);
return 0;
}