this is my demo .
#include <stdio.h>
int sqsum(int a, ...)
{
va_list list;
int b = 0,n = a;
va_start(list,a);
while(n > 0)
{
b = b+n*n;
n = va_arg(list,int);
}
va_end(list);
return b;
}
int main(int argc,char **argv)
{
printf("%d\n",sqsum(1,2,3,-1));
return 0;
}
then I compile this demo , it occurs this error ,I donot know this error mean.
If you did not include #include <stdarg.h> and it does not look like you did, then that would explain the error you are seeing, otherwise the program looks correct. If I do not include that header these are the errors I see using gcc:
In function ‘sqsum’:
13:29: error: expected expression before ‘int’
Related
I'm newly learned about function pointers here but I couldn't define a function pointer to be32toh() or other functions of endian.h header.
First of all, I can do what is told in the given thread:
#include <endian.h>
int addInt(int n, int m) {
return n+m;
}
int main(){
int (*functionPtr)(int,int);
functionPtr = addInt;
return 0;
}
But when I try to do the same for a function like be32toh(), I'll get a compilation error:
#include <stdint.h>
#include <endian.h>
int addInt(int n, int m) {
return n+m;
}
int main(){
int (*functionPtr)(int,int);
functionPtr = addInt;
uint32_t (*newptr)(uint32_t);
newptr = &be32toh;
return 0;
}
Compile as:
$ gcc test.c
Result is as below:
test.c: In function ‘main’:
test.c:15:15: error: ‘be32toh’ undeclared (first use in this function)
15 | newptr = &be32toh;
| ^~~~~~~
test.c:15:15: note: each undeclared identifier is reported only once for each function it appears in
What's the problem and how to fix it?
What's the problem
be32toh is a macro.
how to fix it?
Just write the function yourself.
uint32_t be32toh_func(uint32_t a) {
return be32toh(a);
}
....
newptr = &be32toh_func;
void test(void *a)
{
int *h = a; //error
}
int main(int argc, char const* argv[])
{
int z = 13;
test(&z);
return 0;
}
If I want to keep the void *a for test function, how can I get error line working?
I am able to compile and run the following with gcc without warning or error:
#include <stdio.h>
void test(void *a)
{
int *h = a;
printf("test: %d\n", *h);
}
int main(int argc, char const* argv[])
{
int z = 13;
test(&z);
return 0;
}
The only conceivable reason you would be getting an error on the line you indicated is that you are using a C++ compiler (g++ maybe?). If I try that I get the following error:
error: invalid conversion from ‘void*’ to ‘int*’
If you need to use a C++ compiler, you need to explicitly cast a to an int *:
int *h = (int *) a;
That one change allows this code to compile with g++ as well.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char **argv) {
int one = atoi(argv[1]);
int two = atoi(argv[2]);
int finally;
finally = func(one,two);
printf("%d",finally);
return 0;
}
int func(int first,int second) {
int counter = 0;
int new = first;
while (counter != second)
new = new*first;
counter += 1;
return new;
}
Im very new to coding so a lot of this might look like nonsense,
So this code is a complicated way of using the power arithmetic operation,5*3 == 125,
so if i type (./a.out 5 3) it should give out 125,
i seem to get this error
extension.c:15:19: warning: implicit declaration of function 'func' is invalid
in C99 [-Wimplicit-function-declaration]
finally = func(one,two);
^
1 warning generated.
Move the function declaration before the main() function / or alternatively leave it where it is and simply add a function prototype before the main().
I'm currently coding for a challenge question in a book I'm reading. My code executes perfectly with the correct output, but i"m getting a warning in my code and I'm just wondering why.
I'm getting a warning on the line that reads:
int countdownStart = atoi(numInput);
The warning I'm getting says:
Implicit declaration of function 'atoi' is invalid in C99
#import <readline/readline.h>
#import <stdio.h>
int main(int argc, const char * argv[]){
printf("Who is cool? ");
const char *name = readline(NULL);
printf("%s is cool!\n\n", name);
printf("What should I start counting? ");
const char *numInput = readline(NULL);
int countdownStart = atoi(numInput);
for (int i = countdownStart; i >= 0; i--){
if (i % 3 == 0){
printf("%d\n", i);
if (i % 5 == 0){
printf("Found one!\n");
}
}
}
return 0;
}
You have to include stdlib.h
#include <stdlib.h>
Next time you encounter similar warnings just run man atoi and the manual pages should state that which header file should be included.
Program brief overview (3 body problem):
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
double ax, ay, t;
double dt;
/* other declarations including file output, N and 6 command line arguments */
...
int main(int argc, char *argv[])
{
int validinput;
...
/* input validation */
output = fopen("..", "w");
...
/* output validation */
for(i=0; i<=N; i++)
{
t = t + dt;
vx = ...
x = ...
vy = ...
y = ...
fprintf(output, "%lf %lf %lf\n", t, x, y);
}
fclose (output);
}
/* ext function to find ax, ay at different ranges of x and y */
{
declarations
if(x < 1)
{
ax = ...
}
else if(x==1)
{
ax = ...
}
...
else
{
...
}
if(y<0)
{
...
}
...
}
I get an error on the line '{ /* ext function to find ax, ay at different ranges of x and y */' saying "error: expected identifier or '(' before '{' token"
I think it may be due to not calling or creating the external function in the right way
Your function needs a name! A block of code outside any function is meaningless in C.
There are, in fact, several syntax/conceptual errors in your example. Please clean it up and clarify your question - I'll try to answer better when you've done so.
Now, lets take the following example.
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("hello world \n");
return 0;
}
{
printf("do you see this?!\n");
}
If you compile the above program, it will give you the following error
$ gcc q.c
q.c:10:1: error: expected identifier or ‘(’ before ‘{’ token
$
That is because the gcc compiler expects an identifier before {. So we need to update the above program as follows
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("hello world \n");
return 0;
}
void function()
{
printf("do you see this?!\n");
return;
}
It will work fine.
$ gcc q.c
$ ./a.out
hello world
$
Hope it helps!