How can I accept Char input in C? - c

So i try and compile I get a warning: comparison between pointer and integer [enabled by default] error, what am I doin wrong...?
int main(int argc, char *argv[])
{
char x;
printf("What would you like to do today?\n");
scanf("%s", &x);
if (x == "slm") {
printf("You SLAM a Faygo!");
} else if(x == "kik") {
printf("You KICK the Wicked Elixir!");
}
return 0;
}

Three problems:
You need to read into a string, not a char (with due consideration of buffer overflow), e.g.:
char x[128];
scanf ("%128s", x);
You can't compare strings with ==: you need to use strcmp:
#include <string.h>
...
if (0 == strcmp (x, "slm")) { ... }
You should check scanf's return code for success/failure:
if (1 != scanf (...)) {
perror ("scanf");
return EXIT_FAILURE;
}
(EXIT_FAILURE is defined in stdlib.h.)

x is a char. and "slm" is a string - a char *.
You might want to change that:
char *x;
But now you need to allocate memory. For example:
char *x = (char *)malloc(MAX_LENGTH*sizeof(char));
Also, fvu is right - use if(!strcmp(x,"slm")) instead of if(x ==slm) (strcmp return 0 when the strings are the same)

You're confusing char (which is single character) and char * (which is how C deals with strings)
You cannot meaningfully do == with char *'s the way you want to
You need to:
Change char x to either char *x or char x[1000];
If you use a char *, you will need to use malloc or similar to allocate space
for it
In either case, for production code, you will want to use
something that limits how much data can be read in (e.g. fgets, but
NOT gets).
You will want to use strcmp, which returns 0 if the two
strings are equal and non-zero if not.

Related

Caesar Encryption in C

Hello I am working on a Caesar encryption program. Right now it takes as an input the file with the message to encrypt, the key
The input is currently in this format:
"text.txt", "ccc"
I need to convert this into taking a number so that it fits my requirements, so something like this:
"text.txt", "3"
Then i need to convert this "3" back into "ccc" so that the program still works. The logic being that 3 translates to the third letter of the alphabet "c", and is repeated 3 times. Another example would be if the key entered is "2", it should return "bb".
This is what i have so far but its giving me a lot of warnings and the function does not work correctly.
#include <stdio.h>
void number_to_alphabet_string(int n) {
char buffer[n];
char *str;
str = malloc(256);
char arr[8];
for(int i = 0; i < n; i++) {
buffer[i] = n + 64;
//check ASCII table the difference is fixed to 64
arr[i] = buffer[i];
strcat(str, arr);
}
printf(str);
}
int main(int argc, char *argv[]) {
const char *pt_path = argv[1]; //text.txt
char *key = argv[2]; //3
number_to_alphabet_string((int)key); //should change '3' to 'CCC'
}
Your problem is that you have a function
void number_to_alphabet_string(int n)
that takes an int but you call it with a char*
char* key = argv[2]; //3
number_to_alphabet_string(key);
My compiler says
1>C:\work\ConsoleApplication3\ConsoleApplication3.cpp(47,34): warning C4047: 'function': 'int' differs in levels of indirection from 'char *'
You need
char* key = argv[2]; //3
number_to_alphabet_string(atoi(key));
to convert that string to a number
With char *key = argv[2];, the cast (int) key does not reinterpret the contents of that string as a valid integer. What that cast does is take the pointer value of key, and interprets that as an integer. The result of this is implementation-defined, or undefined if the result cannot be represented in the integer type (a likely outcome if sizeof (int) < sizeof (char *)).
The C standard does not define any meaning for these values.
Here is a test program that, depending on your platform, should give you an idea of what is happening (or failing to happen)
#include <stdio.h>
int main(int argc, char **argv) {
if (sizeof (long long) >= sizeof (char *))
printf("Address %p as an integer: %lld (%llx)\n",
(void *) argv[0],
(long long) argv[0],
(long long) argv[0]);
}
As an example of implementation-defined behaviour, on my system this prints something like
Address 0x7ffee6ffdb70 as an integer: 140732773948272 (7ffee6ffdb70)
On my system, casting that same pointer value to (int) results in undefined behaviour.
Note that intptr_t and uintptr_t are the proper types for treating a pointer value as an integer, but these types are optional.
To actually convert a string to an integer, you can use functions such as atoi, strtol, or sscanf. Each of these have their pros and cons, and different ways of handling / reporting bad input.
Examples without error handling:
int three = atoi("3");
long four = strtol("4", NULL, 10);
long long five;
sscanf("5", "%lld", &five);
number_to_alphabet_string has a few problems.
malloc can fail, returning NULL. You should be prepared to handle this event.
In the event malloc succeeds, the contents of its memory are indeterminate. This means that you need to initialize (at least partially) the memory before passing it to a function like strcat, which expects a proper null terminated string. As is, strcat(str, arr); will result in undefined behaviour.
Additionally, memory allocated by malloc should be deallocated with free when you are done using it, otherwise you will create memory leaks.
char *foo = malloc(32);
if (foo) {
foo[0] = '\0';
strcat(foo, "bar");
puts(foo);
free(foo);
}
In general, strcat and the additional buffers are unnecessary. The use of char arr[8]; in particular is unsafe, as arr[i] = buffer[i]; can easily access the array out-of-bounds if n is large enough.
Additionally, in strcat(str, arr);, arr is also never null terminated (more UB).
Note also that printf(str); is generally unsafe. If str contains format specifiers, you will again invoke undefined behaviour when the matching arguments are not provided. Use printf("%s", str), or perhaps puts(str).
As far as I can tell, you simply want to translate your integer value n into the uppercase character it would be associated with if A=1, B=2, ... and repeat it n times.
To start, there is no need for buffers of any kind.
void number_to_alphabet_string(int n) {
if (1 > n || n > 26)
return;
for (int i = 0; i < n; i++)
putchar('A' + n - 1);
putchar('\n');
}
When passed 5, this will print EEEEE.
If you want to create a string, ensure there is an additional byte for the terminating character, and that it is set. calloc can be used to zero out the buffer during allocation, effectively null terminating it.
void number_to_alphabet_string(int n) {
if (1 > n || n > 26)
return;
char *str = calloc(n + 1, 1);
if (str) {
for (int i = 0; i < n; i++)
str[i] = 'A' + n - 1;
puts(str);
free(str);
}
}
Note that dynamic memory is not actually needed. char str[27] = { 0 }; would suffice as a buffer for the duration of the function.
A cursory main for either of these:
#include <stdio.h>
#include <stdlib.h>
void number_to_alphabet_string(int n);
int main(int argc, char *argv[]) {
if (argc > 1)
number_to_alphabet_string(atoi(argv[1]));
}
Note that with an invalid string, atoi simply returns 0, which is indistinguishable from a valid "0" - a sometimes unfavourable behaviour.
You can't use a cast to cast from a char array to an int, you have to use functions, such as atoi().
You never free your str after you allocate it. You should use free(str) when you no longer need it. Otherwise, it will cause a memory leak, which means the memory that you malloc() will always be occupied until your process dies. C doesn't have garbage collection, so you have to do it yourself.
Don't write things such as char buffer[n];, it can pass the compile of GCC, but it can't in MSVC.
And that isn't the stander way of declaring an array with variable length. use
char* buffer = malloc(n);
//don't forget to free() in order to avoid a memory leak
free(buffer);

Why do I get a segfault with atoi in c?

I'm trying to read a string from argv. It should be something like a mathematical function like "-x^2-5" etc.
I've got something like this:
void test(char *function){
int temp = 0;
for (int i = 0; i < strlen(function); i++){
if((function[i] != '+') && (function[i] != ...){
// function[i] is a digit, but still as a char
temp = atoi(function[i]);
}
}
//...
}
int main(int argc, char **argv){
test(argv[1]);
//...
}
This works quite well until the last lap of the loop.
If I use printf("%c", function[i]);, it sais, it's 5.
But atoi(function[i]) gives a segfault. Why?
Right. Let's first take a look at the signature of atoi.
int atoi(const char *str);
Now, what you are passing to it is function[i], and since function is of type char *, function[i] is a char. And your character is most likely not a proper character pointer.
What you'd want to pass to atoi is instead a const char *, which you can get by calling atoi(&function[i]);
As already said by #sham1, atoi expects its parameter to be a C string, this is a null terminated char array, while you have a single char.
But digits are special, because they are required to have consecutive codes. So if you know that a character (say c) is a digit, its value is c - '0'. So you can write:
if((function[i] != '+') && (function[i] != ...){
// function[i] is a digit, but still as a char
temp = function[i] - '0'; // get the int value of a digit character
}
Besides the bug pointed out in other answers, atoi doesn't have any reliable error handling, which is why it is one of the standard library functions that should never be used.
Just forget that you ever heard about atoi and replace it with:
char* endptr;
int result = strtol(str,&endptr,10);
if(endptr == str)
{
// some conversion error
}
strtol is also able to convert an initial part of the string if it contains valid numbers.

Why can an int return type function return a string?

This is the function I'm talking about:
static int getInput()
{
printf("Enter your name: ");
return fgets(input, sizeof(input), stdin) != NULL;
}
How does this work? Why can an int return type use fgets like this?
"Why can an int return type function return a string?"
In fact, It can´t.
Let´s take a closer look at this statement:
return fgets(input, sizeof(input), stdin) != NULL;
fgets() returns a pointer to char. This pointer is checked for a null pointer. The validation of this boolean expression either evaluates to 1 if the returned pointer is a non-null pointer (fgets() actually returned a pointer to input) or 0 if it is a null pointer - means an error occurred at consuming input by the use of fgets().
This int value (0 or 1) is then returned from the function getInput(); not a pointer or even a string by value itself (which is impossible regarding the C syntax).
Side notes:
input seems to be a global char array. Avoid global arrays. It can confuse you and readers. Pass a pointer to the buffer by reference and also pass the size of the buffer by value instead.
static char* getInput (char* buf, size_t len)
{
if ( len > INT_MAX || len < 2 )
{
return NULL;
}
printf("Enter your name: ");
return fgets(buf, len, stdin);
}
and call it like:
char a[20];
if ( getInput(a, sizeof(a)) == NULL )
{
fputs("Error occured at consuming input!", stderr);
}
For the meaning of the static qualifer in this example, take a look at:
What is a "static" function in C?
fgets(...) != NULL
This evaluates to a boolean and a boolean is represented as an integer in C.
Your function will either return true == 1 or false == 0
This should not return a string
Actually OPs function does not return char * pointer but it is possible to do so
int foo(char *c)
{
return (int)c;
}
it will return char pointer converted to int. if the int is big enough to accommodate the pointer it is possible to convert it back to the pointer.
https://godbolt.org/z/z_xsc7
This kind of conversions is in a very common use in the microcontroller and very low level hardware related programming (when portability does not matter). Unsigned integers are used (usually fixed size like uint64_t to make sure they are big enough) to get, pass and store the addresses

Compare with string in if statement doesn't work

I try to compare my two strings that obtained from my scanf and fscanf. I already figured out what is the content inside the each variable. It both show the same strings, but after I compare with this two string in if statement, it doesn't work and execute else statement instead. What's wrong with my codes ?
int main(void)
{
...
char input[256];
printf("Enter your name: ");
scanf("%s",&input);
fp = fopen(_FILE,"r+");
for(i=0;i<textlines(_FILE);i++)
{
fscanf(fp,"%s %d",stuff[i].name,&stuff[i].salary);
if(input == stuff[i].name)
{
// Update name here
}
else
{
printf("Not Found");
}
}
return 0;
}
== just checks for pointer equality. Use strcmp instead
use the function strcmp in string.h library to compare your strings
As others have said, you need to use strcmp to compare strings (really character arrays). Also, you should not pass the address of name (i.e. &name) to the scanf() function.
You have this:
char input[256];
printf("Enter your name: ");
scanf("%s",&input);
....
if(input == stuff[i].name)
...
More correct code would include the following changes:
char input[256];
printf("Enter your name: ");
scanf("%s", input);
....
if (!strcmp(input, stuff[i].name))
....
You should check the definition and use of stuff[i].name as well. scanf() with a %s format character requires a simple char* parameter. The argument to strcmp() is const char* but using char* is fine and will be automatically promoted.
C is more flexible than other languages in that it allows you to obtain the address of variables. You create pointers to variables in this way. However, a variable declared as an array, such as input is already a pointer in a way. Only by providing an index to you dereference the pointer. Specifically:
char input[256];
input is a pointer to the storage of 256 char's
input can be thought of as a char* variable
input[0] is the first char in the array
input[1] is the second char in the array
input+1 is a pointer to the second char in the array.
input+0 (or simply input) is a pointer to the first char in the array.
&input is not good C form. You can kind of think of this as the address of the array but, in reality, input is already the address of the array. There is a use for such types of double-ly addressed variables but your case is not really one of them. Until you've had some practice with arrays and pointers (and their relationship) the following example might be a bit confusing but it does demonstrate where a char** variable might be used.
int allow_access_to_private_data(char ** data)
{
static char mystring[] = "This is a private string";
if (!data) return -1;
*data = mystring;
return 0;
}
int main(int argc, char* argv[])
{
char* string;
if (!allow_access_to_private_data(&string))
printf("Private data is: %s\n", string);
else
printf("Something went wrong!\n");
return 0;
}

Splitting a string with strtok() goes wrong

I'm trying to get input from the user while allocating it dynamically and then "split" it using strtok.
Main Questions:
Im getting an infinite loop of "a{\300_\377" and ",".
Why do i get a warning of "Implicitly declaring library function "malloc"/"realoc" with type void"
Other less important questions:
3.i want to break, if the input includes "-1", how do i check it? As you can see it breaks now if its 1.
4.In the getsWordsArray() i want to return a pointer to an array of strings. Since i dont know how many strings there are do i also need to dynamically allocate it like in the getInput(). (I dont know how many chars are there in each string)
int main(int argc, const char * argv[])
{
char input = getInput();
getWordsArray(&input);
}
char getInput()
{
char *data,*temp;
data=malloc(sizeof(char));
char c; /* c is the current character */
int i; /* i is the counter */
printf ("\n Enter chars and to finish push new line:\n");
for (i=0;;i++) {
c=getchar(); /* put input character into c */
if (c== '1') // need to find a way to change it to -1
break;
data[i]=c; /* put the character into the data array */
temp=realloc(data,(i+1)*sizeof(char)); /* give the pointer some memory */
if ( temp != NULL ) {
data=temp;
} else {
free(data);
printf("Error allocating memory!\n");
return 0 ;
}
}
printf("list is: %s\n",data); // for checking
return *data;
}
void getWordsArray(char *input)
{
char *token;
char *search = " ,";
token = strtok (input,search);
while (token != NULL ) {
printf("%s\n",token);
token = strtok(NULL,search);
}
}
EDIT:
i noticed i forgot to "strtok" command so i changed it to token = strtok(NULL,search);
I still get wierd output on the printf:
\327{\300_\377
Change:
int main(int argc, const char * argv[])
{
char input = getInput();
getWordsArray(&input);
}
to:
int main(int argc, const char * argv[])
{
char *input = getInput();
getWordsArray(input);
}
with a similar to the return value of getInput():
char *getInput()
{
// ...
return data;
}
In your code, you were only saving the first character of the input string, and then passing mostly garbage to getWordsArray().
For your malloc() question, man malloc starts with:
SYNOPSIS
#include <stdlib.h>
For your getchar() question, perhaps see I'm trying to understand getchar() != EOF, etc.
Joseph answered Q1.
Q2: malloc and realoc returns type void *. You need to explicitly convert that to char *. Try this:
data = (char *) malloc(sizeof(char));
Q3: 1 can be interpreted as one character. -1, while converting to characters, is equivalent to string "-1" which has character '-' and '1'. In order to check against -1, you need to use strcmp or strncmp to compare against the string "-1".
Q4: If you are going to return a different copy, yes, dynamically allocate memory is a good idea. Alternatively, you can put all pointers to each token into a data structure like a linked list for future reference. This way, you avoid making copies and just allow access to each token in the string.
Things that are wrong:
Strings in C are null-terminated. The %s argument to printf means "just keep printing characters until you hit a '\0'". Since you don't null-terminate data before printing it, printf is running off the end of data and just printing your heap (which happens to not contain any null bytes to stop it).
What headers did you #include? Missing <stdlib.h> is the most obvious reason for an implicit declaration of malloc.
getInput returns the first char of data by value. This is not what you want. (getWordsArray will never work. Also see 1.)
Suggestions:
Here's one idea for breaking on -1: if ((c == '1') && (data[i-1] == '-'))
To get an array of the strings you would indeed need a dynamic array of char *. You could either malloc a new string to copy each token that strtok returns, or just save each token directly as a pointer into input.

Resources