char *get;
The above statement defines get
to be a pointer to a character. It can store the address of an object of type char
, not a character itself. The problem is with both scanf
and strcmp
call. You need to define an array of characters to store the input string.
#include <stdio.h> #include <string.h> int main(void) { // assuming max string length 40 // +1 for the terminating null byte added by scanf char get[40+1]; // "%40s" means write at most 40 characters // into the buffer get and then add the null byte // at the end. This is to guard against buffer overrun by // scanf in case the input string is too large for get to store scanf("%40s", get); int k = strcmp("sachin", get); printf("%d", k); return 0; }
Related Posts:
- Pointer Arithmetic
- char *array and char array[]
- C pointers and arrays: [Warning] assignment makes pointer from integer without a cast
- Difference between char* and const char*?
- #31 expression must have integral type
- How do you pass a function as a parameter in C?
- Invalid pointer error on invoking free() after malloc in C
- expression must have integral type
- What’s wrong with my code? What is argv[1]?
- Valgrind: Invalid read of size 1
- Constant pointer vs Pointer to constant
- Reversing a string in C
- Warning: return from incompatible pointer type in C
- Why am I getting this error: “data definition has no type or storage class”?
- warning: passing argument ’from incompatible pointer type [enabled by default]’
- char pointers: invalid conversion from ‘char*’ to ‘char’?
- What is the difference between char array and char pointer in C?
- typedef struct pointer definition
- Dereference void pointer
- The difference between char * and char[] [duplicate]
- lvalue required as increment operand
- What causes a segmentation fault (core dump) to occur in C?
- uint8_t vs unsigned char
- How to use symbols of extended ASCII table in C?
- What is the difference between ++i and i++?
- Using boolean values in C
- What does “collect2: error: ld returned 1 exit status” mean?
- typedef struct vs struct definitions [duplicate]
- Arrow operator (->) usage in C
- What can be the reasons of connection refused errors?
- typedef struct vs struct definitions [duplicate]
- Cannot figure out how to use getchar(); in C
- Why am I getting “void value not ignored as it ought to be”?
- 1 = false and 0 = true?
- Get a substring of a char*
- What causes the Broken Pipe Error?
- Strings and character with printf
- Why does the terminal show “^[[A” “^[[B” “^[[C” “^[[D” when pressing the arrow keys in Ubuntu?
- Why is the gets function so dangerous that it should not be used?
- Write to .txt file?
- What does “collect2: error: ld returned 1 exit status” mean?
- Warning comparison between pointer and integer
- What does #pragma once mean in C? [duplicate]
- What is the difference between const int*, const int * const, and int const *?
- Connect: Socket operation on non-socket
- “Initializer element is not constant” in C
- How do you do exponentiation in C?
- Printing long int value in C
- Undefined reference to pow( ) in C, despite including math.h [duplicate]
- C read file line by line
- Try catch statements in C
- How do I create an array of strings in C?
- How to check if a string is a number?
- The difference between stdout and STDOUT_FILENO
- What does ** do in C language?
- where does stdio.o live in linux machine?
- Using Dynamic Memory allocation for arrays
- Python ? (conditional/ternary) operator for assignments
- conflicting types error when compiling c program using gcc
- Get the current time in C
- What’s the difference between a file descriptor and file pointer?
- How do you pass a function as a parameter in C?
- What is a file with extension .a?
- Cygwin – Makefile-error: recipe for target `main.o’ failed
- Why am I getting “undefined reference to sqrt” error even though I include math.h header?
- Why am I getting “undefined reference to sqrt” error even though I include math.h header?
- Expected declaration specifier error in function
- Multiple definition of … linker error
- Does sizeof return the number of bytes or the number of octets of a type in C?
- Zero an array in C code
- Passing an array by reference in C?
- Excess elements in char array initializer error
- Encrypt with private key and decrypt with public key
- How to generate a random number between 0 and 1?
- What does “pointer being freed was not allocated” mean exactly?
- Initialization from incompatible pointer type warning when assigning to a pointer
- Typedef function pointer?
- How to use timer in C?
- C char* to int conversion
- What is the difference between stdin and STDIN_FILENO?
- Allocating string with malloc
- How to simply convert a float to a string in c?
- hash function for string
- Why use pointers?
- What happens during a “relocation has invalid symbol index” error?
- C: pointer to array of pointers to structures (allocation/deallocation issues)
- What exactly is meant by “de-referencing a NULL pointer”?
- Error: “Access not within mapped region at address” (Valgrind)
- How to convert string to float?
- What is `S_ISREG()`, and what does it do?
- How does the fscanf function work?
- Math constant PI value in C
- Warning: comparison of distinct pointer types
- The code does not execute properly. Try to figure out why
- ld.exe: cannot open output file … : Permission denied
- How to read from input until newline is found using scanf()?
- getline() vs. fgets(): Control memory allocation
- What is char ** in C? [duplicate]
- Segmentation fault- strcat
- segmentation fault with strcpy [duplicate]