Too few arguments to call - c

My teacher isn't willing to help me with my error so I don't know where else to go. On line 19, addition();, the error says that there are too few arguments in the function call and I'm not sure why this is. I am a beginner programmer, but I have called functions before so I'm not sure why I am getting a problem now.
#include <stdio.h>
int addition(int *change);
int main(void)
{
int num = 10;
printf("Name \t Address \t Value\n");
printf("%s \t %p \t %d\n", "num", &num, num);
int *change = &num;
printf("Change: %p\n", change);
*change = 100;
printf("The value of num is %d \n", num);
printf("The value of change is %d \n", *change);
addition();
return 0;
}
int addition(int *change)
{
int input;
int result = input + *change;
printf("Input a value ");
scanf("%d", &input);
printf("The result will be change (%d) + input (%d)\n", *change, input);
printf("Result: %d", result);
return 0;
}

Perhaps this would be better suited to a comment, but I lack the required reputation to post comments...
When you're calling a function, you often have to supply some information. If I walked up to you and commanded "Add!" you might reply "what should I add?" This is essentially what your error message is telling you. You're issuing a command, but you're not giving it enough information to complete that command.
You can find what additional information is required by a function by glancing at its declaration. In this case, your function declaration is:
addition(int *change)
meaning that, in order to function properly, the function requires a pointer to an integer (int *). Every time you call the addition function, you have to supply this argument so that the function knows the number to which it is expected to add.

Related

C program does not recognize my input for 'max'

Hi I keep trying to figuure this out but my input keeps getting ignored, thanks in advance
#include <stdio.h>
#include <stdlib.h>
int main(){
float a, b, a0, b0,i;
char ans;
printf("Fibonacci search method\n\nEnter the function:\n");
printf("\nEnter the intervals over which the Fibonacci method must be applied:\n");
for (i = 1; i <= 1; i++) {
printf("a0 = ", i);
scanf("%f", & a);
printf("bo = ", i);
scanf("%f", & b);
}
printf("Narrow down on either a maximiser or a minimiser (max/min): \n", ans);
scanf(" %c", &ans);
while(ans == 'max'){
printf("maximum selected");
}
printf("minimum selected");
return 0;
}
First of all, you're comparing a single char to a whole string, so you need to modify your ans variable declaration to make it a string, like:
char ans[4]
Keep in mind, this will have a maximum size of 3. If you need to store a bigger string, you'll need to modify this.
Then, after doing this, using a while to do that comparison isn't correct. It's better to implement an if-else. And, inside that, the comparison you're doing is wrong. You need to compare strings, not chars, so you need to use strcmp() function, like:
strcmp(ans,"max") == 0
If this function returns a 0, it means both strings are equal.
Another thing to comment is that you will need to modify your scanf to scan a string, not a char, the new one will be scanf("%3s", &ans);.
And let me tell you one more thing. The for you're using has no sense. You're using a for with parameters i = 1; i <= 1; i++. That means i will start the buckle fulfilling the conditions to break it, so it will only be executed once. In other words, the code inside that for will be executed just once, no matter if it's inside or outside the for.
Anyway, and to sum up, here's your new code:
int main(){
float a, b, a0, b0,i;
char ans[4];
printf("Fibonacci search method\n\nEnter the function:\n");
printf("\nEnter the intervals over which the Fibonacci method must be applied:\n");
for (i = 1; i <= 1; i++) {
printf("a0 = ", i);
scanf("%f", & a);
printf("bo = ", i);
scanf("%f", & b);
}
printf("Narrow down on either a maximiser or a minimiser (max/min): \n", ans);
scanf("%3s", &ans);
if(strcmp(ans,"max") == 0)
printf("maximum selected");
else
printf("minimum selected");
return 0;
}

C - Integer throwing out random value?

Currently I am writing a simple program in C that reads in values the user enters in a loop. For some reason, when I initialize the integer a I am given a random value as opposed to the value I specified. Any help would be greatly appreciated
#include <stdio.h>
int main()
{
char sName[10];
int sTime;
int a = 0;
printf("%d", &a);
printf("Please enter the name of your snail: ");
scanf("%s", &sName);
for(a = 10; a < 20; a = a + 1) {
printf("%d", &a);
printf("Please enter the %d time of your snail: ", &a + 1);
scanf(" %d ", &sTime);
}
return 0;
}
Change this:
printf("%d", &a);
to this:
printf("%d", a);
&a is the address of a (and it's of type int*, so %d is the wrong format). a gives you the value of a.
You still need the & in scanf(" %d ", &sTime);; scanf needs the address of sTime so it knows where to store the value.
You're printing the address of a. You don't want the & in there:
printf("%d", a);
You do want the & for scanf() because you need to tell that function where (at what address) to store the value.

How to "cleanly" terminate the program after buffer overflow attack

I'm studying buffer overflow, and I'm trying to jump to the function 'confused' and then print out "done" at the end of main by performing buffer overflow.
#include<stdio.h>
#include<stdlib.h>
int i, n;
void confused(int i) {
printf("**Who called me? Why am I here?? *** %x\n ", i);
;
}
void shell_call(char *c) {
printf(" ***Now calling \"%s\" shell command *** \n", c);
system(c);
}
void victim_func(){
int a[4];
printf("\nEnter n: "); scanf("%d",&n);
printf("~~~~~~~~~~~~~ values and address of n locations ~~~~~~~~~~");
for (i = 0;i <n ;i++)
printf ("\n a[%d] = %x, address = %x", i, a[i], &a[i]);
printf("\nEnter %d HEX Values \n", n);
// Buffer Overflow vulnerability HERE!
for (i=0;i<n;i++) scanf("%x",&a[i]);
printf("Done reading junk numbers\n")
}
int main() {
printf("\n ~~~~~~~~~~~~~~~~~ Info Menu ~~~~~~~~~~~~");
printf("\n addrss of main %x", main);
printf("\n addrss of shell_cal %x", shell_call);
printf("\n addrss of confused %x", confused);
victim_func();
printf("\n done");
return 0;
}
What I did is I put 7 for n, and for 6th hex value I inserted the address of confused and for 7th the address of printf in main. It successfully prints out "done" after the confused function, but the program goes back to the start of main. I thought the program would terminate after printing out "done".
I just wonder if I did something wrong, or it is the way it should do.
You can always call exit() in your shell code to terminate the program. However, you can't do it using system(), because system() will create a child process which always ultimately return to it parent. You need to directly call exit() using assembly.

Error code: %d expected type int, but argument has type int *

I have an error code I do not understand:
format %d expects type int, but argument 2 has type int *
I do not know the difference between int and int *. I did not know there were different types of int, and cannot find any note of it on webpages about printf and scanf key letters.
The code is as follows:
#include <stdio.h>
#include <math.h>
int main(void)
{
int X = 0, Y = 0, A = 0, D = 0;
printf("This program computes the area of a rectangle ");
printf("and its diagonal length.");
printf("Enter Side 1 dimentions: ");
scanf("%d", &X);
printf("Enter Side 2 dimentions: ");
scanf("%d", &Y);
/* Calc */
A = X * Y;
D = pow(X,2) + pow(Y,2);
D = pow(D, 1 / 2);
/* Output */
printf("Rectangle Area is %d sq. units.", &A);
printf(" Diagonal length is %d.", &D);
return 0;
}
The error references the last two printf's:
printf("Rectangle Area is %d sq. units.", &A);
printf(" Diagonal length is %d.", &D);
Additionally, this program was originally written using floats (declaring X,Y,A, and D as float and using %f). But that gave an even stranger error code:
format %f expects type double, but argument 2 has type float *
I knew that %f is used for doubles and floats, so I could not understand why I had this error. After I got the error code about floats/doubles I tried changing everything to int (as shown in the above code), just to check. But that delivered the error code at the top of this post, which I do not understand either.
I've been using the gcc compiler.
Would someone explain what's being done wrong?
The problem is that you're trying to pass pointers to the printf function. Here's what your code looks like:
printf("Rectangle Area is %d sq. units.", &A);
printf(" Diagonal length is %d.", &D);
A is the int variable, but &A is a pointer to the int variable. What you want is this:
printf("Rectangle Area is %d sq. units.", A);
printf(" Diagonal length is %d.", D);
int* means a pointer to an int object. this is what you get because you use & before the variable name (i.e &A in your code)
You can read this to understand more about pointers and references, but basically if you omit the & before the variable names, it will work fine.
Why passing pointers to printf("...%d...", &D)?
Take a look to pointers explanation:
http://www.codeproject.com/Articles/627/A-Beginner-s-Guide-to-Pointers
And to simplified printf() manual:
http://www.cplusplus.com/reference/cstdio/printf/
int d = 1;
printf("I'm an integer: %d", 42); // OK, prints "...42"
printf("I'm an integer too: %d", d); // OK, prints "...1"
printf("I'm a pointer, I have no idea why you printing me: %p", (void*)&d); // OK, prints "...<address of d>", probably not what you want
printf("I'm compile-time error: %d", &d); // Fail, prints comiper error: 'int' reqired, but &d is 'int*'

How to read variadic function parameters?

This is my code:
#include <stdio.h>
void add(int num, ...);
int main(void)
{
int a=100, b=200, c=300;
add(1, a);
add(2, a, b);
add(3, a, b, c);
return 0;
}
void add(int num, ...)
{
int *p=NULL;
p=&num+1;
printf("%x \n", p);
if(num==1)
{
printf("%d \n", p[0]);
printf("num is: %d \n", num);
}
else if (num==2)
{
printf("%d \n", p[0]+p[1]);
printf("num is: %d \n", num);
}
else
{
printf("%d \n", p[0]+p[1]+p[2]);
printf("num is: %d \n", num);
}
}
From my understanding, p initially points to a, which is 10. Thus, it should print 10, 30, 60, respectively. Nonetheless, it prints
6786db50
1736891264
num is: 1
6786db50
1736924031
num is: 2
6786db50
1867401241
num is: 3
Is p pointing to a wrong address? How can I correctly read the arguments passed as ...?
That's not how you use variadic function calls, you need to use the va_* function calls to extract the parameters.
See http://unixhelp.ed.ac.uk/CGI/man-cgi?stdarg+3 or http://en.wikipedia.org/wiki/Variadic_function#Variadic_functions_in_C.2C_Objective-C.2C_C.2B.2B.2C_and_D
Your particular example may or may not work (Works as you expected on my system). You do p=&num+1; to access the next element. This holds good under the assumption that the stack is ascending which may not be the case with your architecture. And on many systems, variables upto some limit, are passed on registers than on stack! So your assumption goes wrong totally. Also note the variables can be pushed on to the stack either from left to right or the other way. It is unspecified by the standard.
Hence you shouldn't work on assumptions and instead use functions designed for this particular use.

Resources