NULL is not a built-in constant in the C or C++ languages. In fact, in C++ it's more or less obsolete, just use a plain literal 0 instead, the compiler will do the right thing depending on the context.
In newer C++ (C++11 and higher), use nullptr (as pointed out in a comment, thanks).
Otherwise, add
#include <stddef.h>
to get the NULL definition.
c - NUL undeclared- first use in this function - Stack Overflow
C Errors (Beginner) - Stack Overflow
'NULL' undeclared (first use in this function) using android ndk - Stack Overflow
Why am I getting undeclared (first use in this function)
NULL is not a built-in constant in the C or C++ languages. In fact, in C++ it's more or less obsolete, just use a plain literal 0 instead, the compiler will do the right thing depending on the context.
In newer C++ (C++11 and higher), use nullptr (as pointed out in a comment, thanks).
Otherwise, add
#include <stddef.h>
to get the NULL definition.
Do use NULL. It is just #defined as 0 anyway and it is very useful to semantically distinguish it from the integer 0.
There are problems with using 0 (and hence NULL). For example:
void f(int);
void f(void*);
f(0); // Ambiguous. Calls f(int).
The next version of C++ (C++0x) includes nullptr to fix this.
f(nullptr); // Calls f(void*).
There's NULL and then there's NUL.
NULL is defined in stddef.h, is used very widely, and is a reference to a null pointer.
NUL is different - it is the first character in the standard ASCII character set, and more importantly, it is not a standard macro. You may have to define it yourself.
To define NUL, do:
#define NUL '\0'
No, that's not standard. Add this at the beginning of your code or just use 0:
#define NUL 0
I infered you're not looking for NULL since you're doing ch == NUL
In various texts it is quite frequent to refer to '\0' as NUL.
FILE and NULL are wrongly written (C is case sensitive). fopen line was missing a semicolon.
Code below compiles and runs.
#include <stdio.h>
main(void) {
FILE *file = fopen("words.txt","r");
if(file != NULL) {
char line[128];
while(fgets( line, sizeof line, file) != NULL)
{
fputs ( line, stdout );
}
fclose ( file );
}
}
The following works fine and has been cleaned up with consistent braces and spaces in function calls and definitions and loops/ifs. It also works in C, if that was your intent from the tags, and prints an error and returns 1 if the file cannot be opened.
#include <stdio.h>
#include <errno.h>
int main (void) {
char line[128];
FILE *file = fopen ("words.txt", "r");
if (file != NULL) {
while (fgets (line, sizeof line, file) != NULL) {
fputs (line, stdout);
}
fclose (file);
} else {
fprintf (stderr, "Cannot open 'words.txt', error = %d\n", errno);
return 1;
}
return 0;
}
All your JNI calls are missing the first parameter which should be env, as you're in compiling C code.
If your code was in C++, you could make calls like env->NewStringUTF("xx"). But in C, JNIEnv* isn't an object, hence you have to pass it as first argument, like so: (*env)->NewStringUTF(env, "xx")
As for NULL being undefined, you can solve this by including the header that defines it: #include <stddef.h>
The second parameter does not seem to be optional:
jboolean iscopy;
const char *name = (*env)->GetStringUTFChars(string, &iscopy);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct info
{
char *firstName;
char *lastName;
int PUID;
int age;
};
struct node
{
struct info student;
struct node *next;
};
struct node *root = NULL;
struct node *curr = NULL;
void create_list_node( char fName[30], char lName[30], int id, int ages);
int main()
{
return 0;
}
/*
void create_list_no_nodes()
{
root = (struct node*)malloc(sizeof(struct node));
root-> next = 0;
}
*/
void create_list_node( char fName[30], char lName[30], int id, int ages)
{
free(root);
root = (struct node *) malloc(sizeof(struct node));
root->student.firstName = fname;
root->student.lastName = lname;
root->student.PUID = id;
root->student.age = ages;
root->next = NULL; here is the error:
Lab5_main.c: In function ‘create_list_node’:
Lab5_main.c:54:29: error: ‘fname’ undeclared (first use in this function)
root->student.firstName = fname;
^
Lab5_main.c:54:29: note: each undeclared identifier is reported only once for each function it appears in
Lab5_main.c:55:28: error: ‘lname’ undeclared (first use in this function)
root->student.lastName = lname;
^