Why the function calls not getting executed? [duplicate] - c

This question already has answers here:
In C, calling a function from main [closed]
(6 answers)
Closed 4 years ago.
#include<stdio.h>
void check_alphabets(char array_string[],int n){
char alphabet_array[n];
int i,j,val;
int alphabet_array_counter=0;
for(i=0;i<n;i++){
val=array_string[i];
if((val>=65 && val<=90) || (val>=97 && val<=122)){
alphabet_array[alphabet_array_counter]=array_string[i];
alphabet_array_counter++;
}
}
for(j=0;j<n;j++){
printf("%c",alphabet_array[j]);
}
}
void check_number(char array_string[],int n){
char number_array[n];
int i,j,val;
int number_array_counter=0;
for(i=0;i<n;i++){
val=array_string[i];
if(val>=30 && val<=39){
number_array[number_array_counter]=array_string[i];
number_array_counter++;
}
}
for(j=0;j<n;j++){
printf("%c",number_array[j]);
}
}
void check_character(char array_string[],int n){
char character_array[n];
int i,j,val;
int character_array_counter=0;
for(i=0;i<n;i++){
val=array_string[i];
if((val>=32 && val<=47) || (val>=58 && val<=64) || (val>=91 && val<=96) || (val>=123 && val<=127)){
character_array[character_array_counter]=array_string[i];
character_array_counter++;
}
}
for(j=0;j<n;j++){
printf("%c",character_array[j]);
}
}
void main(){
int size=100;
char array_string[size];
printf("Enter string? ");
scanf("%s",array_string);
int i=0,count=0;
while(array_string[i]!='\0'){
count++;
i++;
}
printf("size is %d",count);
void check_alphabets(array_string,count);
void check_number(array_string,count);
void check_character(array_string,count);
}
I have the above piece of code . I don't know what is wrong with it .No matter what I modify The functions are not getting executed.
I am compiling using gcc in ubuntu 18.
But whenever I try to compile it throws a warning which is
In function main
warning:parameter names(without types) in function declaration void check_alphabets(array_string,count)
this warning shows with all the calls from main.
I googled but couldn't find any solution.

These are not function calls:
void check_alphabets(array_string,count);
void check_number(array_string,count);
void check_character(array_string,count);
These are declarations. The return type in front of the function name tells us this is a declaration. When you call a function, you don't need to say what the return type is:
check_alphabets(array_string,count);
check_number(array_string,count);
check_character(array_string,count);

void check_alphabets(array_string,count);
void check_number(array_string,count);
void check_character(array_string,count);
these lines are not function calls. YOu mean
check_alphabets(array_string,count);
check_number(array_string,count);
check_character(array_string,count);
The warning you are getting is because the compiler thinks you are trying to declare a function, but your syntax is an invalid function declaration (argument names without types)

First, you have to learn how to use Markdown to format your code properly. Secondly, you're not calling your functions in main, you are only declaring them.
void check_alphabets(array_string,count);
void check_number(array_string,count);
void check_character(array_string,count);
must become
check_alphabets(array_string,count);
check_number(array_string,count);
check_character(array_string,count);

Related

Pointer as parameter in C

The code doesn't work in my Xcode compiler. It says *&point expected '('. I really don't know what goes wrong. It should have worked.
#include<stdio.h>
#include<stdlib.h>
void transformCopy(int *point);
void transformTrue(int *&point);
int main(){
int *a,*b,i=0;
transformTrue(a);
transformCopy(b);
for(i=0;i<5;i++) {a[i]=i;}
for(i=0;i<5;i++){printf("%d ",a[i]);}
printf("\n");
for(i=0;i<5;i++) {b[i]=i;}
for(i=0;i<5;i++){printf("%d ",b[i]);}
printf("\n");
return 0;
}
void transformCopy(int *point){
point=(int*)malloc(5*sizeof(int));
}
void transformTrue(int *&point){
point=(int*)malloc(5*sizeof(int));
}
*&point expected '('.
References do not exist in C ( void transformTrue(int *&point) ), this is C++ code, not C
If you want to have the equivalent in C you have to use void transformTrue(int **point) and you have to call transformTrue(&a);
If I change your code to do in C what it is done in C++ (see comments) :
#include<stdio.h>
#include<stdlib.h>
void transformCopy(int *point);
void transformTrue(int ** point); /* ** rather than *& */
int main(){
int *a,*b = 0,i=0;
transformTrue(&a); /* &a rather than just a */
transformCopy(b);
for(i=0;i<5;i++) {a[i]=i;}
for(i=0;i<5;i++){printf("%d ",a[i]);}
printf("\n");
for(i=0;i<5;i++) {b[i]=i;}
for(i=0;i<5;i++){printf("%d ",b[i]);}
printf("\n");
return 0;
}
void transformCopy(int *point){
point=(int*)malloc(5*sizeof(int));
}
void transformTrue(int ** point){ /* ** rather than *& */
*point=(int*)malloc(5*sizeof(int)); /* *point = rather than point = */
}
transformTrue(&a) modifies the value of a, but transformCopy(b); does nothing except locally (and a memory leak) and back in main the value of b is still 0, the program will crash when you will try to write in invalid addresses
one possibility is to change transformCopy like that :
int * transformCopy(){
return (int*)malloc(5*sizeof(int));
}
and of course the call to have b = transformCopy();

Error expected ) while passing a pointer member of a structure to a function

I am a beginner to C. In my program i have a structure and a function. I'm trying to pass a pointer which is present in the structure as an argument in my function. But it is showing the error "Expected )" at the dot operator. This is confusing because the other arguments of my function are from the structure as well but this error is not seen in those.
I tried changing the return type of the function to all types and still nothing.
struct signal
{
bool *input;
int previousop;
int n;
}s; //my structure
void noiseremove(bool *input, int n, int count1, int count0, bool
previousop)//function i declared and defined before main function
{//my function here}
void main()
{
void noiseremove(bool *s.input , int s.n, int s.count1, int s.count0, bool
s.previousop); //this is where i call the function and facing an error at
*s.input
}
I'm not sure where i'm going wrong here or if the syntax is wrong. I expect the function to accept the parameters but it isn't.
Having functions inside another function is not possible in C...
So, your code should somewhat look like this:
struct signal
{
bool *input, previousop;
int n, count0, count1;
} s;
void noiseremove(bool *input, int n, int count1, int count0, bool previousop)
{
/* Try using multi-line comments since single-line comments can comment out the end
braces as well...*/
}
void main()
{
/* Initialize the structure before accessing any of its variables, or it will lead
to undefined behavior! */
s = {0};
/* Don't declare the identifiers again... It is 'syntax error' and
your calling convention doesn't in the least look like a calling
convention but more like a function declaration with invalid identifiers
for the parameters... */
noiseremove(s.input , s.n, s.count1, s.count0, s.previousop);
}
struct signal
{
bool *input, previousop;
int n, count1, count0;
} s; //my structure
void noiseremove(bool *input, int n, int count1, int count0, bool previousop) //function i declared and defined before main function
{
// my function here
}
void main()
{
// this should compile but s has not been initialized
noiseremove(s.input , s.n, s.count1, s.count0, s.previousop);
// not sure what you were going for here
//*s.input
}

Calling multiple functions using some generic function call in C

I want to be able to call multiple functions using a single generic function call.
Currently I am trying to implement something like this:
main()
{
generic();
}
A(){.....};
B(){.....};
C(){.....};
Where I call from main() some generic function, which, in turn, is supposed to be able to call the functions: A(), B(), C().
How can I implement this in C?
You can do it by using function pointers. Please refer on "how to use function pointers in C"
You have to use function pointers as below.
#include<stdio.h>
void A(void);
void B(void);
void C(void);
typedef void (*foo_ptr_t)( void );
foo_ptr_t foo_ptr_array[3]; //fucntion pointers of type foo_ptr_t
main()
{
int i = 0;
foo_ptr_array[0] = A; //assign function to each function pointer
foo_ptr_array[1] = B;
foo_ptr_array[2] = C;
for(i = 0; i<3; i++)
foo_ptr_array[i](); //call functions using function pointer
}
void A()
{
printf("We are in A\n");
}
void B()
{
printf("We are in B\n");
}
void C()
{
printf("We are in C\n");
}
You can use function pointers, if all functions you want to call have same prototype.

C Function Pointer Mishap

Okay so I'm trying to learn function pointers. I have a basic function pointer setup like so.
Function to print out linked list:
void seq_print(Seq seq, void (* print_func)(void *)){
Node * p = seq->top;
if(p == NULL){
printf("%s %c", "There is no data to print.", '\n');
return;
}
while(p != NULL){
print_func(p->data);
p = p->next;
}
}
Testing the function:
seq_print(s, printFunc(1));
I get this error:
seq.h:113:32: error: expected declaration specifiers or ‘...’ before ‘(’ token
extern void seq_print(Seq seq, (void *) print_func(void *));
I'm really not sure what to do, any insight would be helpful.
You have two mistakes:
First, notice declaration in error message: in your header file seq.h, declaration of function is wrong!
extern void seq_print(Seq seq, (void *) print_func(void *));
// ^ ^ wrong = parenthesis return type
it should be:
extern void seq_print(Seq seq, void (*print_func) (void *));
// ^ correct ^ = parenthesis function name
Second, at calling place.
seq_print(s, printFunc(1));
// ^^^ you are calling function, and passes returned value
should be:
seq_print(s, printFunc);
// ^^^^^^^^^^ don't call pass function address
My following code examples will help you to understand better (read comments):
#include<stdio.h>
void my_g2(int i, (void*) f(int)); // Mistake: Notice () around void*
void f(int i){
printf("In f() i = %d\n", i);
}
int main(){
my_g2(10, f(1)); // wrong calling
return 0;
}
void my_g2(int i, void (*f)(int)){
printf("In g()\n");
f(i);
}
Check codepad for working code. You can see error is similar to what you are getting:
Line 2: error: expected declaration specifiers or '...' before '(' token
In function 'main':
Line 8: error: too many arguments to function 'my_g2'
Now correct version of this code:
#include<stdio.h>
void my_g2(int i, void (*f)(int)); // Corrected declaration
void f(int i){
printf("In f() i = %d\n", i);
}
int main(){
my_g2(10, f); // corrected calling too
return 0;
}
void my_g2(int i, void (*f) (int)){
printf("In g()\n");
f(i);
}
Now check codepade for output:
In g()
In f() i = 10
Edit: Adding on the basis of comment.
But what if it's like void (*f) (void *) how do I pass in values to that?
From calling function in main() (in my example = my_g2) you need to pass function pointer which you wants call (in my example f()) from function you calls in main (that is my_g2).
You wanted to call f() from my_g2()
We always pass parameters to function at the time of function calling. So if you wants to pass parameters to f() function you have to pass when you call this in my_g2().
A calling expression like below (read comments):
seq_print(s, printFunc(1));
^ // first printFunc(1) will be called then seq_prints
pass returned value from printFunc(1)
is wrong because if you do so seq_print will be called with second paramter value = returned value from function printFunc(1).
To pass void pointer, my following code may help you further:
#include<stdio.h>
void my_g2(void* i, void (*f)(void*));
void f(void *i){
printf("In f(), i = %d\n", *(int*)i);
*(int*)i = 20;
}
int main(){
int i = 10;
my_g2(&i, f);
printf("Im main, i = %d", i);
return 0;
}
void my_g2(void* i, void (*f)(void*)){
printf("In g()\n");
f(i);
}
Output #codepade:
In g()
In f(), i = 10
Im main, i = 20
There's a typo in the forward declaration quoted in the error message. It needs to match the code snippet you posted, with a parameter declaration of void (* print_func)(void *).
Looks like your header has a typo. The declaration should be
extern void seq_print(Seq seq, void (*print_func)(void *));
(void *)print_func(void *) is not a valid function pointer declaration. To declare a function print_func that accepts a void pointer and does not return a value, use void (*print_func)(void *)
EDIT: omitting the parens around (*print_func) does create a function pointer but for a function returning a pointer

why func() and func(void) are different [duplicate]

This question already has answers here:
in c: func(void) vs. func() [duplicate]
(2 answers)
Closed 9 years ago.
I have a function, can write in 2 ways.
void function(void) {
// operations....
}
and
void function() {
// operations......
}
Both functions are of same prototype. Why we have to mention void as argument at function definition?
No, both have different prototypes.
Compile the below programs you will understand.
void function1(void)
{
printf("In function1\n");
}
void function2()
{
printf("In function2\n");
}
int main()
{
function1();
function2(100); //Won't produce any error
return 0;
}
Program 2:
#include <stdio.h>
void function1(void)
{
printf("In function1\n");
}
void function2()
{
printf("In function2\n");
}
int main()
{
function1(100); //produces an error
function2();
return 0;
}
The correct way to say "no parameters" in C and C++ is
void function(void);
But when we write
void function();
It means a little different way in C and C++! It means "could take any number of parameters of unknown types", and in C++ it means the same as function(void).

Resources