Why am I getting "error: use of undeclared identifier" error? - c

Summary
Error and code are at bottom of the question.
I was writing a simple program because I was curious what the size of pointers were and if they differed when they pointed to different data types.
I declared the variables, why are they saying they are undeclared?
Also, for some reason there is no error with the int* but only the bool* and char* as shown in the error message below.
Code
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int* ptri = NULL;
char* ptrc = NULL;
bool* ptrb = NULL;
printf("%lu %lu %lu", sizeof(ptri), sizeof(ptrc), sizeof(ptrb));
}
Error Message
:!clang test.c && ./a.out
test.c:7:5: error: use of undeclared identifier 'bool'
bool* ptrb = NULL;
^
test.c:7:11: error: use of undeclared identifier 'ptrb'
bool* ptrb = NULL;
^
test.c:8:62: error: use of undeclared identifier 'ptrb'
printf("%lu %lu %lu", sizeof(ptri), sizeof(ptrc), sizeof(ptrb));
^
3 errors generated.
shell returned 1

Declare #include <stdbool.h> into the header . It will work.Thanks.

C originally did not have native support for boolean values.
In order to get the things working, you need to import a header file name <stdbool.h>
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int* ptri = NULL;
char* ptrc = NULL;
bool* ptrb = NULL;
printf("%lu %lu %lu", sizeof(ptri), sizeof(ptrc), sizeof(ptrb));
}

The variables are fine (or would be if their declaration were not blocked by other errors).
You have a problem with the type identifier bool. It is not known by (old) standard C.
If you are used to using bool as a type please find out where that type is coming from in your successful other code.

You should write this header " #include <stdbool.h>" which contains type bool
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(void) {
int* ptri = NULL;
char* ptrc = NULL;
bool* ptrb = NULL;
printf("%lu %lu %lu", sizeof(int), sizeof(char), sizeof(bool));
}

Related

undeclared function RegGetValue MinGW C compiler

I am trying to compile a C program using MinGW on Windows 7 (64-bit). The code is given below:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
void readRegDwordValue() {
HKEY hKey = HKEY_LOCAL_MACHINE;
char const *subKey = "Software\\Metpl\\My Program";
char const *pValue = "MJP_XXX";
uint32_t flags = RRF_RT_REG_DWORD;
int *pvData = NULL;
int64_t result = RegGetValue(hKey, &subKey, pValue, flags, NULL, pvData, sizeof(DWORD));
if (result != ERROR_SUCCESS) {
printf("Error getting value. Code: ");
printf("%" PRId64 "\n", result);
} else {
printf("Value data: ");
printf("%" PRId32 "\n", *(int32_t*)pvData);
}
}
int main() {
readRegDwordValue();
return 0;
}
I get the following warning:
gcc -O3 -Wall -c -o readReg.o readReg.c
readReg.c: In function 'readRegDwordValue':
readReg.c:13:22: warning: implicit declaration of function 'RegGetValue'; did you mean 'RegSetValue'? [-Wimplicit-function-declaration]
13 | int64_t result = RegGetValue(hKey, &subKey, pValue, flags, NULL, pvData, sizeof(DWORD));
| ^~~~~~~~~~~
| RegSetValue
I have included windows.h which includes winreg.h that contains the definition of the RegGetvalue function. Why is the compiler not able to find it? Also, since it is suggesting that I meant RegSetValue, does it mean it is able to find this one? !!
The linker gives the following error:
gcc readReg.o -o readReg.exe -L -liphlpapi -ladvapi32
d:/__sdk/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: readReg.o:readReg.c:(.text+0x45):
undefined reference to `RegGetValue'
collect2.exe: error: ld returned 1 exit status
What am I missing here? I have been pulling my hair over this for over 8 hours now and not able to understand where I am making the mistake. I have not been able to find much relevant discussion online on this either.
Desperately request some input on this so that I can move forward. Thanks in advance.
Finally! All the hair-pulling bore fruit. And, all the peripheral learning along the journey now seems exhilarating. Following is what works with MinGW-w64 (32-bit):
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
char buffer[1024]; // Based on need. This is the max that I need.
DWORD bufferSize = sizeof(buffer);
DWORD readRegDwordValue(HKEY hKey, char const *subKey, char const *pValue) {
long unsigned int *p = NULL;
int64_t result = RegGetValue(hKey, subKey, pValue, RRF_RT_REG_DWORD, p, &buffer, &bufferSize);
if (result != ERROR_SUCCESS) {
return 4294967295; // max unsigned int.
}
return ((DWORD *)buffer)[0]; // return the first element of the buffer.
}
char * readStringValue(HKEY hKey, char const *subKey, char const *pValue) {
long unsigned int *p = NULL;
int64_t result = RegGetValue(hKey, subKey, pValue, RRF_RT_REG_SZ, p, &buffer, &bufferSize);
if (result != ERROR_SUCCESS) {
return "Error";
}
return (char *)buffer;
}
int main() {
char *string = readStringValue(HKEY_LOCAL_MACHINE, "Software\\Metpl\\My Program", "companyName");
printf("string:%s%s%d%s \n", string, " (length = ", strlen(string), ")");
DWORD integerValue = readRegDwordValue(HKEY_LOCAL_MACHINE, "Software\\Metpl\\My Program", "sampleInteger");
printf("integer:(%lu%s\n", integerValue, ")");
return 0;
}
I have tested this code and it gives correct results. Since I only need to get the correct result or an error, I have not added additional error-checking in the code.
Thanks, #CGio3, for pointing me to MinGW-w64. I have made many Portable C programs and a few Windows programs with the version I have and got too comfortable with it, I guess. Anyway, lesson learnt.
Hope this helps someone who has been struggling with the subject.

Define a function pointer to be32toh() function

I'm newly learned about function pointers here but I couldn't define a function pointer to be32toh() or other functions of endian.h header.
First of all, I can do what is told in the given thread:
#include <endian.h>
int addInt(int n, int m) {
return n+m;
}
int main(){
int (*functionPtr)(int,int);
functionPtr = addInt;
return 0;
}
But when I try to do the same for a function like be32toh(), I'll get a compilation error:
#include <stdint.h>
#include <endian.h>
int addInt(int n, int m) {
return n+m;
}
int main(){
int (*functionPtr)(int,int);
functionPtr = addInt;
uint32_t (*newptr)(uint32_t);
newptr = &be32toh;
return 0;
}
Compile as:
$ gcc test.c
Result is as below:
test.c: In function ‘main’:
test.c:15:15: error: ‘be32toh’ undeclared (first use in this function)
15 | newptr = &be32toh;
| ^~~~~~~
test.c:15:15: note: each undeclared identifier is reported only once for each function it appears in
What's the problem and how to fix it?
What's the problem
be32toh is a macro.
how to fix it?
Just write the function yourself.
uint32_t be32toh_func(uint32_t a) {
return be32toh(a);
}
....
newptr = &be32toh_func;

expected declaration specifier error , when declaring an external stack

There are three files :
test.c
#include "stack.h"
stackT s2;
StackInit(&s2, 15);
int main(){
stackT s1;
StackInit(&s1, 10);
StackDestroy(&s1);
return 0;
}
stack.h
typedef char stackElementT;
typedef struct {
stackElementT *contents;
int top;
int maxSize;
} stackT;
void StackInit(stackT *stackP, int maxSize);
void StackDestroy(stackT *stackP);
stack.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
void StackInit(stackT *stackP, int maxSize)
{
stackElementT *newContents;
newContents = (stackElementT *)malloc(sizeof(stackElementT) * maxSize);
if (newContents == NULL) {
fprintf(stderr, "Insufficient memory to initialize stack.\n");
exit(1);
}
stackP->contents = newContents;
stackP->maxSize = maxSize;
stackP->top = -1;
}
void StackDestroy(stackT *stackP)
{
free(stackP->contents);
stackP->contents = NULL;
stackP->maxSize = 0;
stackP->top = -1;
}
I really need to have an external stack and
stackT s2;
StackInit(&s2, 15);
in test.c is my attempt to declare that but compiler gives the following error.
test.c:4:11: error: expected declaration specifiers or ‘...’ before ‘&’ token
StackInit(&s2, 15);
^
test.c:4:16: error: expected declaration specifiers or ‘...’ before numeric constant
StackInit(&s2, 15);
^~
Here is what I have tried:
I declared stack s1 inside the main function and didn't get any errors for that.
I deleted #include "stack.h" in test.c and I got
the same error for the external stack s2.
So my questions are:
What is the error for? How can I declare the external stack without any errors?
You can't call functions at top-level in C, you can only have declarations and definitions. Move the call to StackInit() into the main() function.
#include "stack.h"
stackT s2;
int main(){
stackT s1;
StackInit(&s1, 10);
StackInit(&s2, 15);
StackDestroy(&s1);
StackDestroy(&s2);
return 0;
}
A function can only be called from another function. Imagine functions call like a tree in which the root node is the main() function.
Anyway, besides moving the initialization inside main(), if you really want the struct variable definition outside main you can statically initialize it, just doing what StackInit() would do: a stackElementT array of size 15, contents field pointing to the first element, and the top index initialized to -1.
#include "stack.h"
stackElementT elements[15];
stackT s2 = { elements, -1, 15};
int main(){
stackT s1;
StackInit(&s1, 10);
StackDestroy(&s1);
return 0;
}

Why does C give this error ‘size’ undeclared (first use in this function)?

I am a beginner of c programming. I can to write my own malloc function and I put main method code one. It gives following error.
test.c: In function ‘main’:
test.c:38:30: error: ‘size’ undeclared (first use in this function)
test.c:38:30: note: each undeclared identifier is reported only once for each function it appears in
code
/*An horrible dummy malloc*/
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void *malloc(size_t size);
void *malloc(size_t size){
void *p;
p = sbrk(0);
//if sbrk dails , we return null
if(sbrk(size)==(void*)-1){
return NULL;
}
printf("wada\n");
return p;
}
typedef struct s_block *t_block;
struct s_block{
size_t size;
t_block next;
int free;
};
int main(){
malloc(50);
malloc(100);
t_block b;
b = sbrk(0);
sbrk(sizeof(struct s_block)+size);//Error line (code 1)
b->size = size; //Error Line
return 0;
}
In this line: sbrk(sizeof(struct s_block)+size) you use a variable size that you have not defined anywhere. This is clearly stated in the error message.
BTW you should call your function something instead of malloc.

Why can't I call my function(C)?

This is part of a program where I call a function that reads components from a ".dat" file and save the input to members of a Struct. When I try calling the function from my main.c it gives various errors depending on what I try. Most notably: conflicting types of 'ReadFile' and too few arguments to function 'ReadFile'. I also get a warning "passing argument from 'ReadFile' makes integer from pointer without cast" and some infos.
This is main.c
#include "MyData.h"
#include "NodalA.h"
#include "FileHandling.h"
#include <stdio.h>
#include "windows.h"
int main(){
ComponentType *CircuitData;
int numComp = 6;
int numEl = 0;
int numNodes = 0;
CircuitData = malloc((numComp)*sizeof(ComponentType));
ReadFile(CircuitData, &numEl, &numNodes);
return 0;
}
This is FileHandling.c:
#include "FileHandling.h"
#include "stdio.h"
void ReadFile(ComponentType *CircuitData, int *numEl, int *numNodes){
numEl = 0;
numNodes = 0;
int index = 0;
FILE *data;
data = fopen("mydata.dat", "r");
if (data == NULL){
printf("Error: \"mydata.dat\" could not be opened");
}
else {
while(!feof(data)){
fscanf(data, "%s, %s, %s, %f", CircuitData[index].name, CircuitData[index].node1, CircuitData[index].node2, CircuitData[index].value);
*CircuitData[index].node1 = extractInteger(CircuitData[index].node1);
*CircuitData[index].node2 = extractInteger(CircuitData[index].node2);
if(*CircuitData[index].node1 > *numNodes)
*numNodes = *CircuitData[index].node1;
if(*CircuitData[index].node2 > *numNodes)
*numNodes = *CircuitData[index].node2;
numEl++;
index++;
}
}
fclose(data);
}
And this is MyData.h
#ifndef MYDATA_H_
#define MYDATA_H_
typedef struct Comp{
char name[5]; //Name of circuit component
char node1[5], node2[5]; //2 nodes
float value[5]; //value
}ComponentType;
#endif /* MYDATA_H_ */
Any help would be appreciated. There are more code but I think this is the most important part.
The ReadFile function name used in the program is the same as a ReadFile function in "windows.h". The error "too few arguments to function 'ReadFile'" is most likely caused by the program trying to call the the function from windows with the wrong arguments. Removing "windows.h" or renaming the function ReadFile to something else solves the problem.

Resources