Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
char key, vector;
char plainText[101];
char cipher;
int i, cipherValue;
int keyLength, IVLength;
scanf("%s", key);
scanf("%s", vector);
return 0;
}
My program crashes after I input values for the scanf parts. I don't understand why.
The problem with
scanf("%s", key);
scanf("%s", vector);
is:
key and vector are of type char, not pointers to char. The can hold one character only.1
With %s scanf expects a pointer to char. As it stands right now, you
are passing uninitialized integer values as if it were pointers, that's
undefined behaviour and your program crashes as a result of it. The compiler
must have given you a warning about this, don't ignore the compiler's warnings,
they are there to help you, not annoy you.
The correct version:
char key[101], vector[101];
...
scanf("%s", key);
scanf("%s", vector);
// or to limit the number of bytes
// written in the buffer, as pointed out
// in the comments by user viraptor
scanf("%100s", key);
scanf("%100s", vector);
For more information about scanf, please read the documentation
Footnote
1A string in C is a sequence of characters that ends with the
'\0'-terminating byte. A string with one single character needs a char array
of dimension 2 or more. In general, a char array of dimension n can store
strings with maximal length of n-1. You have to keep that in mind when passing
pointers to char to functions when they expect strings. A pointer to a single
char will lead to undefined behaviour because it overflows.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am writing a simple program that takes in user input (a char) and does a simple calculation. However, it segfaults before even hitting scanf.
The other posts I've looked at (below) fix the problem by adding more memory to the char buffer, or passing the address of the variable instead of the variable itself, etc., but that doesn't fix the problem.
Why is this happening and how do I fix it?
other posts I've looked at:
Segfault when using scanf()
C Programming segfault on scanf
SegFault after scanf?
#include <stdio.h>
int main(int argc, const char * argv[]) {
char piece_type;
printf("Enter piece type (k, b, p):\n");
scanf('%c', &piece_type ); //segfaults here
/*other code */
return 0;
}
The first argument to scanf must be a pointer to a string. Most often it is given as a string literal, such as "%c", which is automatically converted to a pointer to its first element.
'%c' is not a string or a pointer to a string. It is a multicharacter constant, which is effectively an int constant.
Enable warnings in your compiler and elevate warnings to errors. With Clang, start with -Wmost -Werror. With GCC, start with -Wall -Werror. With MSVC, start with /W3 /WX.
Instead of the format string in this call of scanf
scanf('%c', &piece_type ); //segfaults here
^^^^
you are using a multibyte integer character constant that has the type int. So the call invokes undefined behavior.
You need to use a string literal
scanf("%c", &piece_type );
Also it is better to include a leading space in the format string like
scanf(" %c", &piece_type );
^^^^
This allows to skip white space characters in the input buffer.
As Eric said, you have to provide string (see reference) int scanf ( const char * format, ... );
One of the following should solve your problem:
replace '%c' with "%c" so the code should look like this
...
printf("Enter piece type (k, b, p):\n");
scanf("%c", &piece_type );
/*other code */
...
use getchar() instead (reference - int getchar ( void );)
...
printf("Enter piece type (k, b, p):\n");
piece_type = getchar();
/*other code */
...
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
This program accept 5 string and print them.
here is the program:-
#include"stdio.h"
#include"conio.h"
void main(){
clrscr();
char s[5];
for(int i=0;i<5;i++){
scanf("%s", s[i]);
}
for(i=0;i<5;i++){
printf("\n\n%s", s[i]);
}
getch();
}
when i execute this program the output will be this
Click here to see the output of the program
but when i enter the string in different way it print wrong output
Click here to see the output of the program
You are reading a string into a char, or rather, the string you read starts at the char position i in s. As s is very short (and when i is 5 it is empty), there will be an overflow, causing undefined behavior.
You want to have an array of strings, not of chars, as Blue Pixy mentions in his comment, e.g. char s[5][32];.
Also turn warnings on. The i in the second for loop is not defined.
You've declared s as a 5-element array of char; each s[i] can store a single character value, not a string. Since you don't explicitly initialize each s[i], they contain an indeterminate value.
The argument corresponding to the %s specifier in scanf must have type char * (each s[i] has type char), and it must point to the first element of an array of char large enough to store the string contents (including the 0 terminator that marks the end of the string).
When you call
scanf( "%s", s[i] );
you're telling scanf to store the next sequence of non-whitespace characters to the address corresponding to the value stored in s[i], which is a) indeterminate and b) likely not valid. The resulting behavior is undefined, meaning pretty much anything can happen - your code may work as expected, it may crash outright, it may give you garbled output, it may corrupt other data, etc.
As written, s can store a string up to 4 characters long.
If you want to store an array of strings, then s needs to be a 2-dimensional array of char:
#define MAX_STRING_LENGTH 20 // or however long you expect your longest string to be
...
char s[5][MAX_STRING_LENGTH + 1];
Each s[i] can now store a string up to MAX_STRING_LENGTH characters. The rest of your code should now behave as expected.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an array of integers, for example:
I memcpy the value into a char buffer, and the next time I retrieve it, it becomes a binary value, for example, 00000 -> 16. How do I avoid this?
Here is a snippet of my code:
char buf[BUFSIZE];
int outgoingPorts[4] = { 100000, 100001, 100002, 100003 };
memcpy(buf, &outgoingPorts[0], sizeof(outgoingPorts[0]);
printf("Port no: %i\n", buf);
Here, buf or the first outgoing port is 16 instead of 10000.
First of all, you're using wrong format specifier altogether, for the content of a char pointer to be printed, you need to use %s, provided that char array in null-terminated. Otherwise, you'll face undefined behaviour.
Then, if you really want to store an int value in a char array, the most suited way to go is to make use of snprintf() to print that value in the char array. Maybe the following pseudo-code will help you
char carr[16];
snprintf(carr, 16, "%d", outgoingPorts[0]);
printf("%s\n", carr); //should print 100000
FWIW, using wrong or mismatched format specifier invokes undefined behaviour.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm having a pretty hard time working on this assignment I have for one of my CSC classes. I am making a character array from my name (ie John Doe). I need to pass a pointer of my character array into a function that outputs the number of characters stored in your character array. Then I need the function to also return this integer back to my main function.
If anyone would like to help me in advanced
I need to spell it completely backwards. i.e. (eoD nhoJ)
I need to inform the user of which element of my array the space in there name is. I also need to return this value as well.
Then last but not least I need to reverse the name i.e. (Doe John)
Please help me with this. I've only posted the part of the assignment I've actually managed to figure out. Thank you all in advance.
#include <stdio.h>
#include <string.h>
int main ()
{
char name [25]={'\0'};
fgets(name,sizeof(name),stdin);
printf("You Entered: %s \n", name);
printf("There are %u characters. \n", strlen(char * name));
}
strlen(char * name) ==> strlen(name)
You should not use char * before string while calculating length
EDIT
If your intention is to write your own function for some practice.
declare function
unsigned int my_strlen(char *); //argument needed is character pointer
And define
unsigned int my_strlen(char *str)
{
//find length..
return length;
}
Function call
printf("There are %u characters. \n", my_strlen(name)); // passing starting address of character array to a function.
Here also you should not use char * before name.
All your questions can easily found on Google. just doing simple search you can found.
If you want to do them by your self. you need to read c tutorials.
Change:
printf("There are %u characters. \n", strlen(char * name));
by:
printf("There are %u characters. \n", strlen(name));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *v= "a";
char *o='e';
char * w='i';
char *e='o';
char *l='u';
char *u[1];
printf ("please enter your character\n");
scanf ("%c",& u);
if (u == v){
puts("the character is it a vowel\n");
}
if (u == o) {
puts("the character is it a vowel\n");
}
else
puts("the character is a constant\n");
system("PAUSE");
return 0;
}
i need help in getting the right answer in finding a vowel from the user input.
First of all, shame on you for ignoring all of the compiler warnings you certainly received. They are there to help prevent you from doing "stupid things."
And why all this nonsense? This is the first of the "stupid things" the compiler is trying to tell you about.
char *v= "a";
char *o='e'; // invalid - Initializing a pointer to a constant 'e' (101).
char * w='i'; // invalid
char *e='o'; // invalid
char *l='u'; // invalid
Are you familiar with how pointers work? If not, I suggest you do some reading and understand them.
The first line makes sense - you're making a string and pointing char* v to that string.
But there's really no point in using pointer for those characters - or even variables at all. Just compare them directly:
char my_character;
if (my_character == 'a') {
// do something
}
And as for reading the character, again, you're using pointers when it doesn't make sense:
char *u[1]; // why?
Instead, just define a single char variable. Now, go look at the documentation for scanf. Giving it a format string of "%c" means, "I want to read just one character". Then, you need to tell where scanf to put it. You do this by passing it the "address of" the variable you want to store it in. You do this with (unsurprisingly!) the address of operator &.
char input_character;
scanf("%c", &input_character);
Armed with this information, you should be able to complete your work. Next, I suggest you look into the switch statement.
Finally, you must use consistent formatting (indentation, spacing) and use meaningful variable names, if you have any desire of ever being taken seriously as a programmer. Spelling out "vowel" for your pointless variables may be "cute" but it's total nonsense.
Most importantly, you should never write a single line of code, unless you understand exactly what it does. If you do this, then do not go asking anyone for help (especially not StackOverflow). If you can't explain what your code is doing (or at least what you think it's supposed to do), then you don't deserve for your program to work.