#include <stdio.h>
#include <stdlib.h>
void message(char m)
{
print("Hello\n");
}
int main()
{
message(m);
}
Error message when I try to compile
danielc#Ubuntu11:$ gcc Messagef.c -o Messagef
Messagef.c: In function ‘main’:
Messagef.c:11:9: error: ‘m’ undeclared (first use in this function)
Messagef.c:11:9: note: each undeclared identifier is reported only once for each function it appears in
I know that am doing a 'silly' mistake but I just see where am going wrong
Your function takes a char parameter but never uses it. The simplest fix is to remove the unused parameter:
#include <stdio.h>
void message()
{
printf("Hello\n");
}
int main()
{
message();
return 0;
}
Alternatively, change your method to use the parameter, and pass in a character as an argument:
#include <stdio.h>
void message(char m)
{
printf("Hello%c\n", m);
}
int main()
{
message('!');
return 0;
}
See it working online: ideone
Declare m in your main (char m = '?';)
Try "printf" instead of "print"
the variable "m" your passing to the message function has not been defined before its passed.
define the m variable above message() or pass a char literal to the function
Your function expects a char and you are passing m without declaring it. You need to declare m first like this:
char m = 'a';
And then call the function. BTW, you are not doing anything with this variable so it is redundant anyway.
Pick up a book of C language and start following that.
alternately u can initialise m with Hello message. pass the pointer to message to function and then print in message function, somewhat like this:
void message(char *msg)
{
printf("%s", msg);
}
int main()
{
char *m = "Hello";
message(m);
return 0;
}
Related
I would like to execute a function given in the paramater of a function. Let me explain with an example.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int object() {
printf("Hello, World!");
return 0;
}
int run_func( int a_lambda_function() ) {
a_lambda_function();
return 0;
}
int main() {
run_func( object() );
return 0;
}
Now, I want to run "object()" in the parameters of "run_func(int a_lambda_function()").
When I run it, It returns an error. How would I achieve this is full C?
Restrictions I have:
Absolutly No C++ allowed.
Functions can be passed as arguments or stored into variables as function pointers.
The definition for a function pointer compatible with your object function is int (*funcp)() ie: a pointer to a function taking an unspecified number of arguments and returning int.
In modern C, functions with an unspecified number of arguments are not used anymore, and functions taking no arguments must be declared with a (void) argument list.
Here is a modified version:
#include <stdio.h>
int object(void) {
printf("Hello, World!");
return 0;
}
int run_func(int (*a_lambda_function)(void)) {
return a_lambda_function(); // can also write (*a_lambda_function)()
}
int main() {
return run_func(object);
}
I have two functions:
void A (void(*fptr)(void*))
void B(void* string)
In main, I am calling function A like so;
char* bird = (char*)malloc(sizeof(char)*100)
strcpy(bird, "bird");
A((*B)(bird)); //error: invalid use of void expression
However, when I try to compile the program, I get an error when calling function A. I'm pretty certain that I am not using the function pointer correctly. Can somebody provide me some guidance?
Probably, your intention is:
#include <stdlib.h>
#include <string.h>
void A(void(*fptr)(void*), void *ptr); // two arguments for A()
void B(void* string);
int main(void)
{
char *bird = malloc(100);
strcpy(bird, "bird");
A(&B, bird); // OR: A(B, bird); which is the same
return 0;
}
I have just written a sample program to understand the working of functions in C. I declared a function in C and call it during my programs execution. However my compiler gives me a warning saying unused function. My code looks like this :
#include <stdlib.h>
#include <stdio.h>
int test_function(x);
int main(){
int x;
char letter[] ={"HAAA"};
char cmpre[] = {"AHD"};
int value;
for(int i=0; i<4;i++)
{
if(letter[i] == cmpre[i])
{
x=0;
}
}
int test_function(x)
{
if (x==0)
{
printf("the letters are the same");
}
return value;
}
printf("To check if the letters are the same go to the function");
test_function(x);
return 0;
}
The program seems to execute fine but I get a warning in the fourth line where I declared the function in the start of the program. The warning is :
Multiple markers at this line
- parameter names (without types) in function declaration [enabled by
default]
- Unused declaration of function 'test_function'
I think the way I am calling my function is not right. Could somebody please help me. Thnak you in advance.
Disclaimer: nested functions are non-standard C and I only know (of) the GNU extension for this. As such anything I claim here may well be untrue in another implementation. My recommendation is that you just don't use them at all.
Your nested test_function is shadowing the global declaration. So the test_function you declared above main is never called, because the call inside main refers to the nested function. Hence, you get a warning.
You should declare int test_function outside of main
for example.
int test_function(int x)
and then call the function in main.
value = test_function(x)
This is what your code should look like:
#include <stdlib.h>
#include <stdio.h>
int test_function(x)
{
int value = 0;
if (x==0)
{
printf("the letters are the same");
}
return value;
}
int main(){
int x = 0;
char letter[] ={"HAAA"};
char cmpre[] = {"AHD"};
int value = 0; // unused
for(int i=0; i<4;i++)
{
if(letter[i] == cmpre[i])
{
x=0;
}
}
printf("To check if the letters are the same go to the function");
test_function(x);
return 0;
}
Note that if you dont need a return value you could make the function void.
And initialize your variables. You may search hours to find such a error
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "scanner.h"
int WhatFell(char *typeoffood)
{
if (strcmp(typeoffood,"meat") == 0);
return 1;
}
void getData(char *typeoffood)
{
printf("What fell on the floor? ");
typeoffood = readToken(stdin);
return;
}
int main(int argc, char **argv)
{
char *typeoffood;
int x;
getData(typeoffood);
x = WhatFell(typeoffood);
printf("%s\n",typeoffood);
printf("%d\n",x);
return 0;
}
eat.c: In function ‘main’:
eat.c:14:12: warning: ‘typeoffood’ is used uninitialized in this function [-Wuninitialized]
getData(typeoffood);
^
A few notes:
'readToken' is found in the "scanner.h" inclusion and simply is a safe version of scanf() for strings.
Also please just focus on the error, this is just a snippet of code I wrote seeings if I would be able to use a function getData for string input in my program.
I'm trying to use a function to ask for user string input (which I can do fine with integers/reals) and then use the string to run another function, but I keep getting all these weird warnings though and if I run it i get a segmentation fault.
char *typeoffood;
int x;
getData(typeoffood);
typeoffood is not initialized but passed to getData() so would receive uninitialised data. NB: The numbers 12:14 in the message tell you the line numbers relevant to the error.
You should pass typeoffood as a pointer to a pointer:
getData(&typeoffood);
and give getData() the prototype:
void getData(char **);
My purpose is to create an array in a function and then return it to the main function.
I was recommended to allocate the array on the heap in the function (otherwise popped from the stack as soon as its returned).
The code is shown below:
#include <stdlib.h>
int main() {
double *v1 = myFunction();
return 0;
}
double *myFunction() {
return malloc(10*sizeof(double));
}
When I compile this code with gcc I get the following error-message:
..\src\main3.c:38:9: error: conflicting types for 'myFunction'
What is wrong with this code? Why do I get this compilation error?
Provide myFunction()'s prototype before using it, that is before main() like so:
double * myFunction();
int main(void)
{
...
If not doing so, the compiler assumes type int for myFunction() when seeing it for the first time.
Than later it finds it to be declared as double * (being different from int) which then provokes the error.
Turning on all warnings would have pointed you to using a function without prototype. Use -Wall -Wextra -pedantic to turn on most of gcc's warnings.
Another issue is that declaring a function without any arguments specified leaves it open what to pass on calling the function:
double * myFunction();
If a function should be specfied to not have any arguments specify void as argument:
double * myFunction(void);
The same for how your code defines main(). It shall be:
int main(void);
or either be
int main(int argc, char ** argv);
1) Add prototype of myfunction at the starting like following or
double *myFunction();
int main() {
double *v1 = myFunction();
return 0;
}
double *myFunction() {
return malloc(10*sizeof(double));
}
2). or define the functionBody before it is called
double *myFunction() {
return malloc(10*sizeof(double));
}
int main() {
double *v1 = myFunction();
return 0;
}
However, first solution is preferable.
Your code is almost fine, there's only one little but blocking error, you are trying to use myFunction before the symbol is defined, all you have to do is :
#include <stdlib.h>
double *myFunction() { // myFunction should be declared before it's used
return malloc(10*sizeof(double));
}
int main() {
double *v1 = myFunction();
return 0;
}
The the open C book states "All identifiers in C need to be declared before they are used. This is true for functions as well as variables." the beauty in C is that you can do can do amazing stuff like this piece of code, but you should follow some guidelines, read the open C book!.
Cheers.
Try this..
return (double*) malloc(10*sizeof(double));