Reputation: 566
Is it possible to return a char Pointer even the calling function is not in the same file.... if I call the function for example:
void gotest(sqlite3 *pt,char *nutzers)
{
char string[1064];
char *text;
text = get_data_byName(pt,"whatever",string);
printf("test %s \n\n\n same file",text);
}
char *get_data_byName(sqlite3 *ptr,char *user,char *resulter)
{
.......
resulter = "TestCall";
return resulter;
}
if gotest(sqlite3 *pt,char *nutzers)
and char *get_data_byName(sqlite3 *ptr,char *user,char *resulter)
are in different files then gcc gives the following
Assignment makes pointer from integer without a cast
if I have both functions in one file everything works fine.
compiling like:
gcc -o test test.c time.c database.c libircclient-1.6/src/libircclient.o -lsqlite3
Upvotes: 2
Views: 372
Reputation: 35788
You need headers for your files. If GCC doesn't see a function prototype in the file it's used in, GCC assumes that the function returns an int
. You need to read up on headers. In the mean time, putting this in the file where you use get_data_byName
will be enough:
char *get_data_byName(sqlite3 *ptr,char *user,char *resulter);
You have to remember that although when GCC is linking, it knows that you define get_data_byName
, when it's compiling, it needs to know what parameters and return values it has. That's the purpose of a header. Headers contain function prototypes. A function prototype looks like this:
char *get_data_byName(sqlite3 *ptr,char *user,char *resulter);
If you put the prototypes of all of the functions defined in one .c
file in one .h
file, and them #include
that header in the files that use the function, you wont get that error anymore. So if you have a file called get_data_by_name.c
, this would be get_data_by_name.h
:
#ifndef GET_DATA_BY_NAME_H
#define GET_DATA_BY_NAME_H
char *get_data_byName(sqlite3 *ptr,char *user,char *resulter);
#endif
Then just #include
it in the file that uses get_data_byName
:
#include "get_data_by_name.h"
Upvotes: 2
Reputation: 782
Depends how you have them "in separate files." The format specified in K&R would require 2 extra .h files for each .c file you have. The .h files would declare the function prototypes. From there, you can include the file_2.h into your file_1.c, and make get_data_byName available to gotest. Hope that helps.
**file_1.c**
include "file_1.h"
include "file_2.h"
void gotest(sqlite3 *pt,char *nutzers)
{
char string[1064];
char *text;
text = get_data_byName(pt,"whatever",string);
printf("test %s \n\n\n same file",text);
}
**file_2.c**
char *get_data_byName(sqlite3 *ptr,char *user,char *resulter)
{
.......
resulter = "TestCall";
return resulter;
}
**file_1.h**
void gotest(sqlite3 *pt,char *nutzers);
**file_2.h**
char *get_data_byName(sqlite3 *ptr,char *user,char *resulter);
Upvotes: 2
Reputation: 400069
Yes, it's certainly possible. You need to have a declaration of the function before calling it:
char * get_data_byName(sqlite3 *ptr, char *user, char *resulter);
Also, you should really use const
for pointers where applicable.
And remember that if you need to return a non-literal string, you need to think about memory management.
Upvotes: 2
Reputation: 122001
There must be no declaration of get_data_byName()
visible to gotest()
, so the compiler generates an implicit declaration and assigns a return type of int
, hence the warning. To resolve add a declaration for get_data_byName()
, in a header file for example, and ensure it is visible to gotest()
.
get_data.h
:
#ifndef GET_DATA_HEADER_H_
#define GET_DATA_HEADER_H_
extern char* get_data_byName(sqlite3 *ptr,char *user,char *resulter);
#endif
Then #include <get_data.h>
in the .c
file where gotest()
is defined.
Upvotes: 2
Reputation: 726987
You get the error not because the functions are in different files: it is perfectly fine for functions from different compilation units to call each other. The real problem is that you are trying to call a function from the other file without providing a prototype. When this happens, C assumes a return type of int
.
Make a header file with this content:
get_data_byName.h
char *get_data_byName(sqlite3 *ptr,char *user,char *resulter);
Now include this header file in both of your C files:
#include "get_data_byName.h"
The compilation warning will go away.
Upvotes: 2