499 questions
2
votes
3
answers
275
views
Why void() redefines a function in C++?
Given this function:
int f() {return 10;}
void(f()) defines a void f() and doesn't compile with the following error:
'void f(void)': overloaded function differs only by return type from 'int f(void)'...
5
votes
1
answer
173
views
Local scope constant as function's default argument
Can function declaration in a function scope have a locally defined constant as default argument?
For example,
void f(int) {}
int main() {
constexpr int c = 1;
void f(int = c);
f();
}
...
4
votes
6
answers
271
views
Function returning 101 and -101 instead of 1 and -1
So my professor gave us the assignment to code a function that works exactly like strcmp() but without using any library functions. I came up with the following code but instead of returning 1 or -1 ...
0
votes
2
answers
94
views
Avoid names of variables ending into function definitions in R
I would like to use values within variables for defining functions in R. However, after declaring the function I can still see the variable names instead of their values inside the function definition....
4
votes
2
answers
141
views
Function definition with prototype vs without prototype
There are (source):
void f(); // declaration (1)
void f(void); // declaration with prototype (2)
void f() { ... } // definition (3)
void f(void) { ... } // definition with ...
2
votes
3
answers
98
views
Understanding Function Definitions that Return a Function Pointer
I am hoping to get some help with understanding why function definitions, with function pointer return types need to be written the following way.
int ( * functionFactory (int n) ) (int, int) { ...
5
votes
2
answers
187
views
Is it standard C17 to wrap a parameter in a function declaration in parenthesis
Is the following a standard C function declaration according to ISO/IEC 9899:2017 (c17)?
int foo(int (bar), int (baz));
If so, please point me to the section in the standard that defines this.
In ...
2
votes
1
answer
157
views
How to declare a const and non-const operator overload in one declaration (templately)?
INTRO
I am writing a class stalker<Obj> that holds inside a variable of type Obj. I want that stalker<Obj> to pretend that it is almost the same as Obj variable (from the user's ...
1
vote
1
answer
63
views
Can the function itself serve as a parameter to another function?
this is my code:
#include <stdio.h>
int getsum(int a,int b){
return a+b;
}
void fun1(int a,int b,int (*c)(int a,int b)){//Standard writing method
int sum=getsum(a,b);
printf("%...
4
votes
2
answers
405
views
C function: array argument length
This question builds on this one, which describes how the following are equivalent:
int f(int a[10]) { ... } // the 10 makes no difference
int f(int a[]) { ... }
int f(int *a) { ... }
In ...
2
votes
1
answer
56
views
Renaming/re-declaring functions for CFFI?
I have a C library for which I'm trying to create (out-of-line, API mode) CFFI bindings. The C library provides various implementations of each function, but all of them have this giant, obnoxious ...
1
vote
2
answers
193
views
char** v *char[] in C
As I read through C code, I often times see this:
int main(int argc, char **argv)
but I always learned to do this:
int main(int argc, char *argv[])
So, why would I use one over the other? I know ...
3
votes
3
answers
316
views
Defining function pointer in C?
Recently I came across a surprising way of defining a function pointer in C:
typedef void (func_type)(void);
func_type *func_ptr;
Is this a correct way of defining a function pointer?
If we define ...
1
vote
2
answers
149
views
C function type without a typedef
Suppose we have many C functions with the same signature and return type:
R f(X x,Y y){...}
R g(X x,Y y){...}
R h(X x,Y y){...}
where X and Y are the types of the arguments and R is the type of the ...
91
votes
4
answers
9k
views
What's the significance of a C function declaration in parentheses apparently forever calling itself?
In gatomic.c of glib there are several function declarations that look like this:
gboolean
(g_atomic_int_compare_and_exchange_full) (gint *atomic,
gint ...
6
votes
2
answers
182
views
Why does declaring the same function both with and without parameters not cause compilation errors?
For the code:
int hi(int);
int hi();
int main()
{
hi(3);
}
I don't get any compilation errors (calling hi(); without arguments does get a compilation error).
I expected that the compiler would ...
0
votes
2
answers
112
views
passing structure to function in c language
can anyone help? why '&' is not required while calling a function in this program? but is thought that '&' is required in call by reference.
#include <stdio.h>
#include <stdlib.h>
...
1
vote
1
answer
97
views
How can I pass array to function argument? [duplicate]
I have this code:
#include <stdio.h>
void replaceIndexElement (int index, int element);
int main ()
{
replaceIndexElement(2, 3);
return 0;
}
void replaceIndexElement (int index, int ...
2
votes
1
answer
88
views
Storing a pointer to an array of C strings
Goal:
In a C module that manages display of text, I'd like to store the pointer to an array of strings, so that I can feed the module different arrays to display.
I don't want to copy the array, just ...
0
votes
1
answer
176
views
Can namespace functions be declared at block scope?
Can namespace functions be declared at block scope outside the namespace they were defined at?
This code does not compile when DECLARED_IN_NS is defined as 1:
#define DECLARED_IN_NS 1 // can be ...
1
vote
2
answers
97
views
How to define functions from declarations where the parameters do not have identifiers?
For an assignment, I was given a header file. The objective was to write the function definitions in a C file.
I am confused about how to write the definition for the functions when some of them do ...
1
vote
4
answers
131
views
How can I replace the auto reference keyword in this code? [duplicate]
hello i want to find a way to replace auto keyword in the following code.
#include <iostream>
using namespace std;
void printMatrix(const auto & matrix) {
/* print matrix using range-...
1
vote
1
answer
55
views
Javascript in Node.js shows bizarre behaviour involving functions and block scopes
Can anyone make sense of the difference in behaviour between the two snippets described below?
Snippet #1
{
function f() {return 1}
f = function() {return 2}
function f() {return 3}
}
...
2
votes
1
answer
111
views
C++ string modification, no pass by reference? [duplicate]
I am a beginner studying C++. Currently I am studying functions, C strings, and pass by reference.
The program below is meant to take a string from input, replace any spaces with hyphens, and any ...
0
votes
1
answer
73
views
How to update field of a pointer to a struct in C?
Im trying to update a field of a struct that's a pointer like so:
typedef struct Data *STRUCT_POINTER;
typedef struct Data
{
int element;
STRUCT_POINTER next;
} STRUCT_NODE;
void manipulate(...