Warning: Variable is unitialized in this function - c

#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 **);

Related

Segmentation fault (core dumped) error while passing string to bool function

I keep getting this error when I try to pass these char values to bool function:
#include <stdio.h>
#include <stdbool.h>
bool checksub(char *s1, char *v) {
if (*s1==*v){
return true;
}
}
int main(void) {
printf(checksub("a","a"));
}
Is that why I keep getting this error or is it a different reason?
segmentation fault (core dumped)
Your code has two problems. First of all, the checksub function has no return value in case the condition is false. The second problem is that in printf you are missing the format of the value you would like to print.
This is the working code:
#include <stdio.h>
#include <stdbool.h>
bool checksub(const char *s1, const char *v) {
return (*s1 == *v);
}
int main(void) {
printf("%d\n", checksub("a","a"));
}
I think that in C the best way to compare string is usign the strcmpstandard function. So, the checksub becomes:
bool checksub(const char *s1, const char *v) {
return !strcmp(s1, v);
}
I believe it has to do with using printf with bools
I am not sure who you compiled the program because it gave me errors with:
tmp.c:11:10: error: incompatible type for argument 1 of ‘printf’
which hints to the problem you should call printf with strings if you want to print bools use any of these methods here:
What is the printf format specifier for bool?
if you want more details about printf use https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm

Getting "error: invalid use of void expression" when trying to use a function pointer

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;
}

53:6: warning: conflicting types for function

#include <stdio.h>
#include <string.h>
struct students{
char name[50];
int age;
int height;
};
int main(int argc, char **argv)
{
struct students manoj;
strcpy(manoj.name, "manojkumar");
manoj.age = 15;
displaymanoj(&manoj); //print testing \n , name , age
return 0;
}
void displaymanoj(struct students *ptr) {
printf("Testing...............DEBUG\n");
printf("%s\t%d\n", ptr->name,ptr->age);
printf("END OF TEST: SUCESS -manoj-");
}
I am learning C and it's working where is use pointer to point to structure variable. I am getting the correct output when I run the program. Just that my Geany IDE giving out some message which I would like to know why.
My Compiler Message as below :
You must declare the functions before calling them.
So your program should look something like
// Includes
// Structure
// Function prototype declaration
// This was what you were missing before
void displaymanoj(struct students *ptr);
int main(int argc, char **argv)
{
...
}
void displaymanoj(struct students *ptr) {
...
}
Since you have the definition of displaymanoj() isn't seen when you call it from main(), compiler implicitly declares one with return type int
which conflicts with the actual one. Note that the implicit declaration has been removed since the C99 standard and is no longer valid.
To fix it:
1) Either move the function displaymanoj() above main()'s definition or
2) Do a forward declaration of displaymanoj().

why does the compiler give a warning for unused function?

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

C Hello World Program in a subroutine

#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;
}

Resources