This question already has answers here:
What is C local function declaration mechanism?
(3 answers)
Closed 5 years ago.
#include <stdio.h>
void main()
{
static int array[5] = { 200, 400, 600, 800, 1000 };
int sum;
int addnum(int *ptr); // what happened here ?
sum = addnum(array);
printf("Sum of all array elements = %5d\n", sum);
}
int addnum(int *ptr)
{
int index, total = 0;
for (index = 0; index < 5; index++)
{
total += *(ptr + index);
}
return(total);
}
int addnum(int *ptr); //Exactly what does this code mean ?
Just the topic or name of the concept would be fine . Thanks a lot in advance .
int addnum(int *ptr); // what happened here ?
It is so called forward declaration allowing compiler to know that such function will be defined later.
In C it is possible (although unwise) to implement a pair of mutually recursive functions thus:
int first(int x) {
if (x == 0)
return 1;
else
return second(x-1); // forward reference to second
}
int second(int x) {
if (x == 0)
return 0;
else
return first(x-1); // backward reference to first
}
Related
Hi extremely new to programming. Just want to know if for example I have a function called void blink(void). If I write a code
2 * blink();
is it the same as
blink();
blink();
?
2 * blink();
No, this will not call blink() 2 times, this will multiply 2 with the value that blink() returns. Because blink() is void (that means blink() doesn't return anything), this will cause an error.
I have a variable 'num' and I want the function to be called as much
as the num value
You should use a for loop. For example:
for(int i = 1; i <= num; ++i)
/* this is the same as for(int i = 0; i < num; ++i) */
{
blink();
}
I do not know where you took this syntax from, but it will not call the function twice. It will multiply the function return value by two.
int blink(void)
{
return 5;
}
int main(void)
{
int result;
result = 2 * blink();
printf("%d\n", result);
}
https://godbolt.org/z/4nrGj3M6W
if blink has void return type it will simple not compile.
Another way of multiple calling function using function pointer:
void blink(void)
{
printf("BLIMK!!!\n");
}
void callMultiple(void (*func)(void), size_t ntimes)
{
while(ntimes--) func();
}
int main(void)
{
size_t times;
if(scanf("%zu", ×) == 1)
{
callMultiple(blink, times);
}
}
https://godbolt.org/z/jWEd9hnK9
This question already has answers here:
Assigning a pointer to an integer
(6 answers)
Closed 4 years ago.
I learn C programming for a while and I had to create a program, which contains a function void hello() displays word 'Hello :)' and the number, how many time the function hello() was called. The code bellow displays 'Hello' but the number of function calling stays constant. I just want to know, what's wrong and why it isn't working as it should.
#include <stdio.h>
int main(void) {
void hello(int *p_number);
int number = 1, i;
int* p_number = number;
for (i = 1; i <= 10; i++){
hello(&p_number);
printf("Number in cyclus = %d\n", number);
number++;
}
return 0;
}
void hello(int *p_number){
printf("number of calling = %d, Hello :)\n", *p_number);
}
You need
int* p_number = &number;
and
hello(p_number);
at the calling site.
i.e. set p_number to the address of number. And do turn up the warning level on your compile and read them! There is a fair amount of redundancy in maintaining a pointer in hello; presumably this is for an exercise?
#include <stdio.h>
void hello(int *p_number);
int main(void) {
int number = 1, i;
int *p_number = &number;
for (i = 1; i <= 10; i++) {
hello(p_number);
printf("Number in cyclus = %d\n", number);
number++;
}
return 0;
}
void hello(int *p_number) {
printf("number of calling = %d, Hello :)\n", *p_number);
}
Set the pointer to point at the address of number, pass pointer address to function.
I'm trying to recursive function in C that calculates the sum of the numbers from x to max (inclusive). For example, sum (4, 7) would compute 4 + 5 + 6 + 7 and return the value 22. The function code a must be recursive so you are not allowed to use any conventional loop constructs.
I have this and i think it should work but i'm not entirely sure why its not
#include <stdio.h>
int main()
{
int sum (x, max);
int total, y, x, max;
if (x<max){
y=x+1;
total = x+sum(y,max);
return total;
return x;
}
return 0;
}
Thanks for any help with this in advance!
Here is one possible solution:
#include <stdio.h>
int sum_in_range(int a, int b){
if(a != b){
return sum_in_range(a+1,b)+a;
}
else{
return b;
}
}
int main(void) {
// your code goes here
printf("%d",sum_in_range(2,4));
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int sum(int s,int max)
{
if(s==max)
{
return s;
}
else
{
return(s+sum(s+1,max));
}
}
int main()
{
int r,s,max;
printf("\n enter s and max");
scanf("%d%d",&s,&max);
r=sum(s,max);
printf("%d",r);
}
I spotted some errors on your code. I'm not a pro yet but here's what I think
I just edit your code. removed, added and rearranged some stuff*/
/*First, let's look at your code*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sum(x, max);//I think what you want to do here is declare a function but instead declaring, you define it here because you added semicolon (;)
int total, x, y, max;
if(x < max)
{
y = x + 1;
total = x + sum(y, max); //you don't have a function declaration for "sum"
return total;
return x; //this will never return since you already "return the total before this"
}
return 0;
}
//////////////////////////////////////////////////////////////
/*And I think this is what you want to do*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x = 4, max = 6; //We declare this here for the "sum" function. This way "sum" function can recognize these variables
int total = x; //be sure to set the total to x.
//you can make a void function for this instead of "int". but either way, it can do the job.
void sum(int y) //no need to pass a "max" because "max" is already recognized by the "sum" function since we declare the variables at the top of "main" function
{
if(x < max)//don't make it x <= max. Because of the argument "total = total + (x + 1)" on this function. If you do, the total will exceed.
{
//You can see here why we set the value of "total" to x.
total = total + (x + 1);//And also, you can see why we didn't make the argument in if() statement like this: if(x <= max).
x++;//increment "x" every loop
//call the function again and pass the total until x == max.
sum(total);
}
}
//pass the x
sum(x);
//check the answer
printf("The total is %d\n\n", total);
return 0;
}
//////////////////////////////////////////////////////////////
/*It looks messy with comments*/
/*Here's the code looks like without a comment.It's pretty short code if you look remove the comments.. LOL..*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x = 4, max = 6;
int total = x;
void sum(int y)
{
if(x < max)
{
total = total + (x + 1);
x++;
sum(total);
}
}
sum(x);
//check the answer
printf("The total is %d\n\n", total);
return 0;
}
This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 6 years ago.
I'm really confused about the result of the following code:
#include <stdio.h>
#include <stdlib.h>
int one(int a, int b) {
int k, t;
k = a - b;
t = a + b + 1;
if (k % 2 == 0) return t;
else return 0;
}
int two(int x, int y) {
int m;
printf("%d\n", m);
return m + x + y;
}
main() {
int result = two(5, one(4, 3));
// printf("%d\n", one(4, 3));
printf("result is %d\n", result);
}
one(4, 3) returns 0, which is not surprising. But I don't understand why two(5, 0) returns 8. In other words, m takes on the value 3 without being initialized. How did this happen?
C does not automatically initialize values to 0 when you define them. Technically, reading that data before you initialize it is undefined behavior. In practice, this normally results in a garbage value containing whatever data was stored in that location previously.
This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 8 years ago.
How is the most basic function pointer created in C?
Here's a pretty straightforward example:
#include <stdio.h>
#include <string.h>
int add(int x, int y) {
return x + y;
}
int sub(int x, int y) {
return x - y;
}
int main() {
char const * operator = "addition";
int (*operation)(int, int) = NULL;
if (strcmp(operator, "subtract") == 0)
operation = sub;
if (strcmp(operator, "addition") == 0)
operation = add;
if (operation) {
int x = 3;
int y = 7;
int z = operation(x, y);
printf("%s %d %d == %d\n", operator, x, y, z);
}
}
Where is this useful?
A common use case that I've seen is to create interfaces. So, for example, we have a type of object, say a struct that represents a network interface. Now, because there are different hardware backend, we might want to implement the functions differently depending on the hardware implementation.
So we might have a function pointer in the interface struct for initializing the hardware, sending packets, and receiving packets.
This code will demonstrate how to create and modify a basic function pointer
#include <stdio.h>
int my_function(int);
int (*function)(int)=&my_function;
int my_function2(int x){
function=&my_function; // & is optional
return (0);
}
int my_function(int x){
function=my_function2;
return (x);
}
/* And an example call */
int main(){
printf ("%d \n",function(10));
printf ("%d \n",function(10));
printf ("%d \n",function(8));
return 0;
}