how to call particular function using #define macro? - c

I want to call a particular function according to the value i passed to the macro. But it is giving me compilation error
#include <stdio.h>
#define calling(m, j) execcall ## m(j);
void execcall0 (int x) {
printf("called 0 with arg %d\n", x);
}
void execcall1 (int x) {
printf("called 1 with arg %d\n", x);
}
void execcall2 (int x) {
printf("called 2 with arg %d\n", x);
}
int main () {
int i = 0;
for (i = 0; i < 3; i++) {
calling(i, 1);
}
}
Compilation error:
In function `main':
new.c:(.text+0x7a): undefined reference to `execcalli'
collect2: ld returned 1 exit status
Is it even possible whatever i am trying?

If you want to call a function based on the value of an integer you're better off writing an array of pointers to your functions, and indexing into the array using your integer.
void execcall0(int x);
void execcall1(int x);
void execcall2(int x);
/* Array of pointers to void functions taking an int parameter. */
void (*apfn[])(int) =
{
execcall0,
execcall1,
execcall2,
};
int main()
{
int i;
for (i = 0; i < 3; ++i) {
(apfn[i])(1);
}
}
Remember to check the boundary conditions before you index!

You can use an array to store functions, and modify the macro:
#include <stdio.h>
#define calling(m, j) exec_funcs[m](j)
void execcall0 (int x) {
printf("called 0 with arg %d\n", x);
}
void execcall1 (int x) {
printf("called 1 with arg %d\n", x);
}
void execcall2 (int x) {
printf("called 2 with arg %d\n", x);
}
void (*exec_funcs[3])(int) = { execcall0, execcall1, execcall2 };
int main () {
int i = 0;
for (i = 0; i < 3; i++) {
calling(i, 1);
}
}
But then you don't really need a macro.

Related

CS50 Plurality using voters_count

I thought it would be really practical to use the int voter_count from the main function also in my print_winners function. But since the int voter_count is not introduced before the main function as the int candidates_count is, for example, I am not able to use the voter_count int.
When I introduce the int before main and remove the int before voters_count in the main function when it is getting called, then my code works and all scenarios I tried worked correctly.
But I know that we were not supposed to change code in the main function and even with the changed main function my code still does not pass the check50.
Does anyone know why my code is not passing the check50?
void print_winner(void)
{
for (int c = voter_count; (c > 0); c--)
{
int u = 0;
for (int j = 0; (j < candidate_count); j++)
{
if (candidates[j].votes == c)
{
u++;
printf("%s \n", candidates[j].name);
}
}
if (u != 0)
{
return;
}
}
}
Response for check:
voter_count = get_int("Number of voters: ");
Here I changed the main function by removing the int before the voter_count because
//Numver of votes
int voter_count;
I introduce the program to the int above the header file.
Functions in C can use either global variables (but please don't), local variables, or their arguments. A local variable in one function cannot be accessed in another.
E.g.
void foo(void);
int main(void) {
int bar = 42;
foo();
return 0;
}
void foo(void) {
printf("%d\n", bar);
}
That will not work. However, we can pass bar as an argument to foo.
void foo(int bar);
int main(void) {
int bar = 42;
foo(bar);
return 0;
}
void foo(int bar) {
printf("%d\n", bar);
}
The following will work, but you shouldn't use it.
int bar;
void foo(void);
int main(void) {
bar = 42;
foo();
return 0;
}
void foo(void) {
printf("%d\n", bar);
}

undefined reference to ERROR `get_squares'

I have the following simple program I am trying to compile in Dev-C++
main.c
#include <stdio.h>
#include "squares.h"
int main() {
void get_squares(int n, int output[]);
int n = 10;
int squares[10]; // Declare an array large enough to hold 10 integers
get_squares(n, squares); // Use get_squares to populate the array
// Print each element of the array
int i = 0;
for (i = 0; i < 10; i++) {
printf("%d\n", squares[i]);
}
puts("Hello, world!");
return 0;
}
squares.c
#include "squares.h"
void get_squares(int n, int output[]) {
int i = 0;
for (i = 0; i < n; i++) {
// Modifies the array passed as input
output[i] = i * i;
}
}
squares.h
#ifndef SQUARES_H
#define SQUARES_H
void get_squares(int n, int output[]);
#endif
Why do I get the following error? I tried to add void get_squares(int n, int output[]); in the main but it does not work.
hello.c:(.text+0x1e): undefined reference to `get_squares'
collect2.exe: error: ld returned 1 exit status

f(&a) is possible to run in C?

The explanation below confused me:
When an argument is pointer to a variable x, we normally assume that x will be modified :
f(&x);
It is possible, though, that f merely needs to examine the value of x, not change it.
I tired to understand and the code below can't work.
#include <stdio.h>
void function(int& a)
{
a = 5;
}
void func(int b)
{
b = 5;
}
int main(void)
{
int x = 0;
function(x);
printf("%d", function(x));
func(x);
printf("%d", func(x));
return 0;
}
Code refer from the second answer:
int f(int &a){
a = 5;
}
int x = 0;
f(x);
//now x equals 5
int f2(int b){
b = 5;
}
int y = 0;
f2(y);
//y still equals 0
An example actually using f(&x):
#include <stdio.h>
void f(int *p) {
*p = 4;
}
int main(void) {
int x;
f(&x); // Provide a pointer to `x`.
printf("%d\n", x); // 4
return 0;
}
Both of your program use int &a, which isn't a valid C declaration. That is why they don't even compile.

Creating macro to execute a single operation n times

In my code I often have a for loop for doing a single operation n times. E.g:
// Wait for settle
int delayLoop = 0;
int count = 0;
for(delayLoop = 0; delayLoop < count; delayLoop++) {
__NOP(); // operation to do
}
At first I wanted to make this as an function....but then I realized I do not know how to pass in the operations as a function argument.
In the above example the __NOP() is itself a macro that expands to:
__ASM volatile ("nop")
So how can I come up with a macro that I can call like this:
DO_LOOP(10, __NOP)
and what if I need to do more operations? e.g.
DO_LOOP(8, __NOP, myFunction(arg1))
that would expand to:
for(int i = 0; i < 8; i++) {
__NOP;
myFunction(arg1);
}
#define DO_LOOP(x, ...) for (int i = 0; i < x; ++i) { __VA_ARGS__; }
void f1() { printf("test\n"); }
void f2() { printf("a\n"); }
int main()
{
DO_LOOP(10, f1(), f2());
return 0;
}
gcc -E test.c:
void f1() { printf("test\n"); }
void f2() { printf("a\n"); }
int main()
{
for (int i = 0; i < 10; ++i) { f1(), f2(); };
return 0;
}
This doesn't work with inline assembly though. You can do something like:
#define DO2(x, a, b) for (int i = 0; i < x; ++i) { a; b;}
and use:
DO2(10, __NOP(), f1());

Warning parameter names [When I try to load my array

So I'm getting this message when I try to load my array using pointers.
I don't know why this keep appearing since the last program had no problem
#include<stdio.h>
#define T 10
void FLoad(int *);
void main () {
int a[T];
void FLoad(a);
}
void FLoad(int *a) {
int x;
for (x = 0; x < T; x++)
scanf("%d", a+x);
}
And here is a little program that works perfectly
#include <stdio.h>
void FImp(int *, int );
main () {
int a[] = {-10,-5,3,4}, tam;
tam = sizeof(a) / sizeof(int);
FImp(a, tam);
}
void FImp(int *a, int t) {
int x;
for (x = 0; x < t; x++)
printf("%d ",*(a + x));
putchar('\n');
}
You are using incorrect syntax when calling your function
void main()
{
int a[T];
void FLoad(a);
}
should be
void main()
{
int a[T];
FLoad(a);
}
or even better
int main(void)
{
int a[T];
FLoad(a);
}
You don't specify the function return value when you call it.
void FLoad(a);
This won't call the function. The compiler will consider this as a function declaration. So call the function without void it will work fine.

Resources