Expected identifier while trying to compile - c

Trying to do cs50 without the cs50 GetString.
Got stuck by doing the new function included in the following code :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
void PrintName(char name);
{
printf("Your name is %c\n", name);
}
int main();
{
char fio[10];
printf("Hello, ");
scanf("%c", &fio);
PrintName(fio);
return 0;
}
Says the next:
hello-0.c:9:1: error: expected identifier or '('
{
^
hello-0.c:14:1: error: expected identifier or '('
{
^
What it could be?

First things first, you have extra semi-colons: in front of main and PrintName function. Remove them.
Secondly, you created char array (aka string) and you scanf-ed it wrong. If you want to get name as string (not as a char, as you did), you have to do it like this:
char fio[10];
printf("Hello, ");
scanf("%9s", fio);
Note that, as I am reading string, my format is %s (note 9 in there to read up to 9 characters, because you have array of 10). Moreover, I pass the address of my char array (which is already an address). That's how you read a string. And printing it in function will be:
void PrintName(char name[])
{
printf("Your name is %s\n", name);
}
where we pass char[] to function and again print with %s format

Related

How to get string as return value from function in C?

I am a newbie in C and am trying to make a hangman game where a player will have to guess a random word selected by the program. But I am stuck in getting the word. I tried a lot and found some answers on SO but could not relate it to my case.
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char dictionary[3][15] = {"food","cat","coder"};
//15 is max length of each word
char getWord() {
srand(time(0));
char *random_elem = dictionary[rand()%3];
printf(random_elem);
return random_elem;
}
void gamePlay() {
*word = getWord();
printf(*word);
return;
}
int main() {
printf("Welcome to Hangman\n");
printf("------------------------------------\n\n");
gamePlay();
}
The printf in the getWord() works but not in gamePlay()
The following error is generated:
<stdin>:11:12: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
printf(random_elem);
^~~~~~~~~~~
<stdin>:11:12: note: treat the string as an argument to avoid this
printf(random_elem);
^
"%s",
<stdin>:12:12: error: cannot initialize return object of type 'char' with an lvalue of type 'char *'
return random_elem;
^~~~~~~~~~~
<stdin>:17:6: error: use of undeclared identifier 'word'; did you mean 'for'?
*word = getWord();
^~~~
for
<stdin>:17:6: error: expected expression
<stdin>:18:13: error: use of undeclared identifier 'word'
printf(*word);
^
1 warning and 4 errors generated.
OS: Android 11
App: Cxxdroid
If that might help
The right code is the above.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char dictionary[3][15] = {"food","cat","coder"};
//15 is max length of each word
char* getWord() {
srand(time(0));
char *random_elem = dictionary[rand()%3];
printf("%s\n",random_elem);
return random_elem;
}
void gamePlay() {
char *word = getWord();
printf("%s\n",word);
return;
}
int main() {
printf("Welcome to Hangman\n");
printf("------------------------------------\n\n");
gamePlay();
}
In this case you can return a pointer, since the actual strings are declared with static storage duration (if they are local variables, you can't do that). You should however const-qualify the pointer since string literals are read-only. Fixed code:
const char* getWord() {
static const char dictionary[3][15] = {"food","cat","coder"};
srand(time(0));
const char *random_elem = dictionary[rand()%3];
puts(random_elem);
return random_elem;
}
I moved the variable declarations inside the function since global variables should be avoided. static ensures that they still have static storage duration so you can return a pointer to them.
The warnings about "format string is not a string literal" is nothing to be concerned with in this case. If you replace printf with puts they should go away.

Unable to return function parameter due to conflicting types for 'function' in C

I'm new to C and I'm trying to understand how there are conflicting types for my function "using_name".
I also don't understand why I have to include a '*' to name the 'using_name()' function. It is because I'm storing a value in the function address?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int using_name(), call_func(char name[20]);
char name[20];
int main ()
{
using_name();
call_func(name);
return 0;
}
char* using_name()
{
printf("What is your name: ");
scanf("%s", name);
printf("\n Your name is %s", name);
return name;
}
int call_func(char name[20])
{
printf("Hello %s", using_name(name));
}
Error: conflicting types for 'using_name'
The return and argument types in the funnction prototype at the beginning of the program has to match the actual types when the function is defined later.
Since using_name() is defined as:
char *using_name()
You need to change the earlier prototype to:
char *using_name();
int call_func(char name[20]);
Another solution is to just put the function definitions at the beginning of the program. You only need prototypes for functions that are used before they're defined, or functions that are defined in another compilation unit (although these prototypes are usually put in a header file).

What is "error : invalid use of void expression"?

I am trying to create a hexadecimal to base64 converter. I don't know if I am going in the right direction converting binary as I am attempting to do now or if there is a more direct way of converting. Any suggestions on the math of converting or how to code hex to base64 would be very helpful.
On the contrary, I have been receiving the error(The new one since updated code):
hexto64.c: In function ‘main’:
hexto64.c:21:17: error: lvalue required as left operand of assignment
ReVerse(input) = RevHex;
^
If anybody could help explain what this error means and how to fix
would help greatly! Thank you in advance.
EDIT: So thanks to the few people in the comments, I now understand the error.
Here is my code(Updated):
#include <stdio.h>
#include <math.h>
#include <string.h>
char ReVerse(const char *str)
{
if (*str != '\0')
ReVerse((str + 1));
printf("%c", *str);
}
int main()
{
char RevHex;
char input[4096] = {0};
printf("Enter Hexadecimal: ");
scanf("%s", input);
RevHex = ReVerse(input);
printf("\n");
return 0;
}
Last edit: I have found the error in my code. Thank you guys for all the feedback!
You try to use the return value of the function ReVerse() but this function return void. It's "nothing" so you can't assign "nothing" to something. Here you try to put your array RevHex to "nothing". This don't make sense.
If your function is just reversing the entered Hexadecimal string then you can do it like this:
#include <stdio.h>
#include <math.h>
#include <string.h>
void ReVerse(const char *str) {
if (*str != '\0')
ReVerse((str + 1));
printf("%c", *str); }
int main() {
char input[10] = {0};
printf("Enter Hexadecimal: ");
scanf("%s", input);
ReVerse(input);
printf("\n");
return 0; }
Here I do not think that this char RevHex[4096] = {0}; is useful. Because you want to reverse the input string. If you implement it like this then this program will work.
Talking about your program, you are assigning a string value to a void function, that is why it is giving the error.
I don't know how to "fix" this but trying to assign RevHex to a function will throw an error.
Given that ReVerse doesn't return anything, I am not sure what RevHex is for...
The error is this line: ReVerse(input) = RevHex;
You can't assign something to a call of void function, you should pass it to the function as parameter.

creating a function; scanf and char issues

I am at the very initial stage of learning programming. I working with a program that uses self-made function. I don't understand my mistakes. I would be grateful for your help. Please, do me a favor and answer using methods that are commensurate with the primitive stage I am at. I am leaving comments I've written, so you can see what I am trying to achieve by this or that code line.
/* Prints a user's name */
#include <stdio.h>
#include <string.h>
// prototype
void PrintName(char name);
/* this is a hint for the C saying that this function will be later specified */
int main(void)
{
char name[50];
printf("Your name: ");
scanf ("%49s", name); /* limit the number of characters to 50 */
PrintName(name);
}
// Says hello to someone by name
void PrintName(char name)
{
printf("hello, %s\n", name);
}
I get these error messages:
function0.c: In function ‘main’:
function0.c:14: warning: passing argument 1 of ‘PrintName’ makes integer from pointer without a cast
function0.c: In function ‘PrintName’:
function0.c:21: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
function0.c:21: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
The function PrintName is based on the previous program I took from the course (and adopted it to C):
#include <stdio.h>
#include <string.h>
int main (void)
{
char name[40];
printf ("Type a name: ");
scanf ("%39s", name);
printf ("%s", name);
printf("\n");
}
This last program works perfectly.
What mistake do I make in my original program? If I understand correctly, there is a problem with my PrintName function.
The initial program that prints a name is a modified version of CS50 program that uses CS50 library:
// Prints a user's name
#include <stdio.h>
#include <cs50.h>
// prototype
void PrintName(string name);
int main(void)
{
printf("Your name: ");
string s = GetString(); //GetString is the same as scanf; it takes input from the user
PrintName(s);
}
// Says hello to someone by name
void PrintName(string name)
{
printf("hello, %s\n", name);
}
Given "string" is "char" in C, I replace string with char in my program.
Thank you!
You should use char* instead of char, as an argument of your function. Char is one symbol, while char* is pointer to a string.
Your function PrintName is waiting for a char as parameter but you give a char[] that's why you see this :
warning: passing argument 1 of ‘PrintName’ makes integer from pointer without a cast
To give a char[] as parameter you need to change your function like this :
void PrintName(char *name);
Change void PrintName (char name); to void PrintName (char *name); or to void PrintName (char name[]);
Currently this function receives one character called name. You want it to receive an array of chars.
void PrintName(char name);
You function expects a character variable to be passed but instead you pass a char array. Thus it leads to error.
Moreover, %s in printf will expect a char * and you pass a char,thus leading to another problem.
To correct you program declare and define function with parameter as follows-
void PrintName(char *name);
or
void PrintName(char name[]);
Both will work.

invalid type argument of '->' (have 'int')

I get the error reported below while I am compiling my code. Could you please correct me where I mistaken?
invalid type argument of -> (have int)
My code is as follows:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
typedef struct bundles
{
char str[12];
struct bundles *right;
}bundle;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
unsigned long N;
scanf("%lu", &N);
bundle *arr_nodes;
arr_nodes = malloc(sizeof(bundle)*100);
int i=5;
for(i=0;i<100;i++)
{
scanf("%s", &arr_nodes+i->str);
printf("%s", arr_nodes+i->str);
}
return 0;
}
I am facing issues at these lines:
scanf("%s", &arr_nodes+i->str);
printf("%s", arr_nodes+i->str);
You mean
scanf("%s", (arr_nodes+i)->str);
without parentheses the -> operator was being applied to i instead of the increased pointer, that notation is often confusing, specially because this
scanf("%s", arr_nodes[i].str);
would do exactly the same.
You should also, check that malloc() didn't return NULL and verify that scanf() did scan succesfully.
You need
scanf("%s", (arr_nodes+i)->str);
printf("%s", (arr_nodes+i)->str);
Your original code was the same as
scanf("%s", &arr_nodes+ (i->str) );
because the -> has a higher precedence than +, so you get that error.
As per the operator precedence, -> is having higher precedence over +. You need to change your code to
scanf("%s", (arr_nodes+i)->str);

Resources