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);
Related
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
void pedir_diag(int* diag){
scanf("%i", &diag);
}
int main() {
int number;
pedir_diag(&number);
return 0;
}
When I compile introduce an integer and expect that number int variable in the main have the value introduced but is not set
I have this, in my code but i am not able to set diag variable with scanf() function.
diag is already a pointer so no need for the address-of operator (&) in the call to scanf():
void pedir_diag(int *diag)
{
scanf("%d", diag);
}
But to do it that way is kinda stupid. You have no way to check for erroneous input.
Better:
#include <stdbool.h>
bool pedir_diag(int *diag)
{
return scanf("%d", diag) == 1; // *)
}
*) scanf() returns the number of successful conversions, so if the number of successful conversions is 1 we return true.
Usage:
#include <stdio.h>
int main(void)
{
int foo;
if (!pedir_diag(&foo)) {
fputs("Input error :(\n\n", stderr);
}
else {
// have fun with foo
}
}
The best way to use scanf through your entire coding life is by remembering the following rule
scanf("%[direction_of_what_to_scan][special format character]",
Pointer_on_some_memory_location);
What scanf does is that it stores what the input was (with some length restrictions) to a memory address. So, either:
int n1;
scanf("%d",&n); //which means the address of variable n1 in memory
or
int *n2 = &n1;
scanf("%d",n) // &doesn't need to be used cause n2 is now already pointer to an integer
Both will are different implementations of the same thing, they differ to the part of using pointers,for which C is well-known,and even applicable these days.
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.
I used Eclipse to compile c code, but all of a sudden all my codes got trouble, which were all correct previously.
for example if I want use scanf input a argument, before the scanf a printf statement I will use for guiding the user. like printf("type the size\n"); but after compiling in Console I need type the size first, then the printf("type the size\n") command just pop up, which should be the other way round.
#include <stdio.h>
#include <stdlib.h>
void try(int a);
int main(void)
{
int a;
printf("type the size\n");
try(a);
return 0;
}
void try(int a)
{
scanf("%d", &a);
printf("%d\n", a);
}
the result:
2
type the size
size is chosen 2
I need type a number first, here like I need type 2 first and then the "type the size" just pop up.
here is what I want :
type the size
2
size is chosen 2
Its a bug in eclipse and this has been reported by most of the people using eclipse and MinGW.
To overcome this problem, you could use fflush(stdout) after every call to printf or use the following in the start of main :
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
This will cause stdout and stderr to flush immediately whenever it is written to.
try this one, for example,
#include <stdio.h>
#include <stdlib.h>
void try(int *a);
int main() {
int a;
printf("type the size\n");
fflush(stdout);
try(&a);
return 0;
}
void try(int *a){
scanf("%d", a);
printf("%d\n", *a);
fflush(stdout);
return;
}
Also, you need to get the value set in atry back in main, so, you need to pass a a pointer as shown
I'm working on my assignment for my C course, and I'm trying to take in the user's input and store it in a variable to use for later in my code. Here's what my main function looks like,
int main() {
// Variables here
char* inputLine[10];
do {
printf("Insert number....");
scanf("%s\n", inputLine);
// More stuff here
}
return 0;
}
This code gives me a bunch of warnings, warning: format specifies type 'char *' but the argument has type 'char **' [-Wformat], and if I change the variable declaration to,
char* inputLine = NULL;
When I execute my code I get a seg fault, can someone explain to me what I am doing wrong, and the differences of what happens in the memory when I'm initializing this variable?
char* inputLine[10];
--> is an array of ten pointers to char
printf's format %s expects argument of type char *, but you're providing it as type char **
Just use
char inputLine[10];
To avoid possible buffer overflow you should use
scanf("%9s", inputLine); //Notice the size with %s
9 only because C string are null terminated ('\0') so one extra byte for it goes at end
char inputLine[10];
do {
printf("Insert number....");
scanf("%9s\n", inputLine);
// More stuff here
} while( //some condition);
However if you edit your code and remove * you get answer, but normal array deprecated, nowdays, programmers use vector, normal array in C not safe :
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> inputLine;
You can define with every data type:
vector<int> myvar;
Or you can define multidimensional vector:
vector< vector <int> > myvar;