Author |
Message |
Mathew Hend #1 / 27
|
 const char* vs. char *
: int may be signed or unsigned Incorrect of course: int is always signed. I was thinking of char. But may signed int have a different representation than int? -- Mat.
|
Mon, 07 Feb 2000 03:00:00 GMT |
|
 |
Jarrod Driscol #2 / 27
|
 const char* vs. char *
Hello my name is Jarrod Driscoll, I have a problem. I wrote this program and compiled it and it works however I keep on getting the following warning messages. I was wondering if there was anyway around this. The following warning messages I recieved are warning return makes integer from pointer without a cast. warning assignment makes pointer from integer without a cast. a segment of same code that I have written is a follows : openfile( char directory[] , char filename[], FILE *filep) { chdir(directory); if ( filep = fopen(filename, "r") == NULL ) { printf("Error cant open file \n"); exit(1); } else printf("File Open \n"); return(filep); Quote: }
readfile( FILE *filepointer) { char line[200]; fgets(line,200,filepointer); printf("%s\n" , line ); Quote: }
main () { FILE *filepointer, *fp; char directory[50], filename[50]; filepointer = (openfile(directory,filename,fp)); readfile(filepointer); Quote: }
What happens is the openfile procedure changes to the directory specified and opens the file, then returns the file pointer back to the filepointer filepointer which then gets passed to the readfile procedure which just gets some information and outputs it to the screen. Is there any way to program out the warnings as there must be some thing that I am doing wrong ! If any one has some ideas they would be greatly appreciated... THANKS FOR YOU TIME JD.
|
Tue, 08 Feb 2000 03:00:00 GMT |
|
 |
Cliff Rhode #3 / 27
|
 const char* vs. char *
Quote: > I have difficulty choosing the right keyword to define a var type in a > function. i.e, I don't know when I should use (char *) and when (const > char *), (int) and (size_t). > 1.Take the standard ANSI functions: > char *strcat(char *dest,const char *src); > // why is it that dest is a char pointer and src is a const char > pointer?//
Because no attempt will be made to change *src. So it is const--that tells the user that *src will be unchanged by the call. *dest on the other hand will be changed--so it cannot be const. Quote: > int strcmp(const char *s1,const char *s2); > // why in this case both s1 and s2 are pointers to const char?//
Neither *s1 or *s2 are changed by the compare. So, they are const. The caller can be sure that their contents after the call are exactly the same as before. Quote: > 2. In traditional c facilities: > the strncar() fn is declared as: > char *strncat(char *dest, char *src, int n); > why does ANSI C change it to: > char *strncat(char *dest,const char *src,size_t n); > What's the difference btw (char *) vs. (const char *)
If your function will not change the data pointed to be an argument, always make it const. If your function might, or will change what the pointer points to, it cannot be const. and (int) vs. Quote: > (size_t). and under what situations should I opt for one over another?
size_t is really unsigned int. It is used in places where the value could never be negative. It is the type returned by the sizeof() operator. You should use it whenever an argument needs to be some positive byte count. Use int for general integer arguments that can be positive or negative.
|
Tue, 08 Feb 2000 03:00:00 GMT |
|
 |
Paul Morr #4 / 27
|
 const char* vs. char *
On Fri, 22 Aug 1997 14:59:37 +1000, Jarrod Driscoll Quote:
>The following warning messages I recieved are >warning return makes integer from pointer without a cast. >warning assignment makes pointer from integer without a cast. >openfile( char directory[] , char filename[], FILE *filep) >{ <removed code> > return(filep); >}
You have not given a return type for the function openfile. In C, if no return type is given then the compiler defaults to a return type of int. But you return a FILE*. Therefore what the compiler is warning you about is true, when you call the function it returns an int, which you assign to a pointer (FILE *). To stop this happening you NEED to give return values to all your functions. The above function would become: FILE *openfile( char directory[] , char filename[], FILE *filep) { } Also if you want a function with no return value, use void, eg void functWithNoReturnValue() { } <removed code> Quote: >main () >{ >FILE *filepointer, *fp; >char directory[50], filename[50]; >filepointer = (openfile(directory,filename,fp)); >}
Hope this helps. From Paul Morris Software Engineer GEC Marconi Radar And Defence Systems
These are my thoughts, I think.
|
Tue, 08 Feb 2000 03:00:00 GMT |
|
 |
David LaR #5 / 27
|
 const char* vs. char *
Jarrod, If you do not explicitly specify the return value for a function in C, it will default to the type int. The function openfile does not have a return type specified to the compiler presumes that it will be an int. At the end of openfile the return statement attempts to return a FILE * into the int value. Your compiler issues a warning to this effect. To remove the warning complete the specification for openfile as FILE * openfile( char directory[], char filename[], FILE *filep) { .. Quote: }
This will remove the warning from the return satement. The second warning would be on the "filepointer = openfile" line in main (). The warning is issued because the function "int openfile (...)" returns an int and that value is being saved into filepointer which is declared as a FILE *. The second warning will also be removed by the completed declaration of openfile (). Good luck, David LaRue Team OS/2, ACM, IEEE
Quote: >Hello my name is Jarrod Driscoll, I have a problem. I wrote this program >and compiled it and it works however I keep on getting the following >warning messages. I was wondering if there was anyway around this. >The following warning messages I recieved are >warning return makes integer from pointer without a cast. >warning assignment makes pointer from integer without a cast. >a segment of same code that I have written is a follows : >openfile( char directory[] , char filename[], FILE *filep) >{ > chdir(directory); > if ( filep = fopen(filename, "r") == NULL ) > { > printf("Error cant open file \n"); > exit(1); > } > else > printf("File Open \n"); > return(filep); >} >readfile( FILE *filepointer) >{ >char line[200]; >fgets(line,200,filepointer); >printf("%s\n" , line ); >} >main () >{ >FILE *filepointer, *fp; >char directory[50], filename[50]; >filepointer = (openfile(directory,filename,fp)); >readfile(filepointer); >} >What happens is the openfile procedure changes to the directory specified >and opens the file, then returns the file pointer back to the filepointer >filepointer which then gets passed to the readfile procedure which just >gets some information and outputs it to the screen. Is there any way to >program out the warnings as there must be some thing that I am doing >wrong ! If any one has some ideas they would be greatly appreciated... >THANKS FOR YOU TIME >JD.
|
Tue, 08 Feb 2000 03:00:00 GMT |
|
 |
David Z. Ma #6 / 27
|
 const char* vs. char *
JD> Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c,comp.lang.c++ Is it C or C++? fopen() and such are features of C; followups set to comp.lang.c and alt.comp.lang.learn.c-c++. JD> I keep on getting the following warning messages. I was wondering JD> if there was anyway around this. The following warning messages I JD> recieved are warning return makes integer from pointer without a JD> cast. warning assignment makes pointer from integer without a JD> cast. JD> openfile( char directory[] , char filename[], FILE *filep) What type does openfile() return? You never specify. I suspect from the return statement that it should be FILE *openfile(char directory[], char filename[], FILE *filep) but unless you explicitly state this the compiler will assume you meant "int". The return statement then tries to turn filep into an int, which is defined but causes a (correct) warning. JD> { JD> JD> chdir(directory); JD> if ( filep = fopen(filename, "r") == NULL ) If you completely ignore the filep that's passed into the function, why pass it? filep's use here could be just as easily done with a local variable. JD> { JD> printf("Error cant open file \n"); JD> exit(1); In ANSI C, exit() can only return 0, EXIT_SUCCESS, or EXIT_FAIILURE. JD> } JD> else JD> printf("File Open \n"); JD> JD> return(filep); JD> } JD> JD> readfile( FILE *filepointer) What does readfile return? Anything? You should explicitly declare it as a void function if it doesn't. JD> { JD> char line[200]; JD> JD> fgets(line,200,filepointer); JD> printf("%s\n" , line ); JD> } JD> JD> JD> main () Either int main(void) or int main(int argc, char **argv). JD> { JD> FILE *filepointer, *fp; JD> char directory[50], filename[50]; JD> JD> filepointer = (openfile(directory,filename,fp)); fp's only purpose here is to be passed to openfile(), which subsequently ignores its (completely random) contents. It should probably go away. It would also be a really good idea to somehow get something into directory and filename before you call openfile(); right now, they're complete garbage. JD> readfile(filepointer); You should, for propriety's sake, fclose(filepointer); JD> } -- _____________________________ / \ "...electronic spam is about as appealing | David Maze | as the luncheon meat of the same name..."
| http://donut.mit.edu/dmaze/ | -- _The San Jose Mercury News_, \_____________________________/ August 8, 1997
|
Tue, 08 Feb 2000 03:00:00 GMT |
|
 |
Denis Constal #7 / 27
|
 const char* vs. char *
Quote:
> Hello my name is Jarrod Driscoll, I have a problem. I wrote this program > and compiled it and it works however I keep on getting the following > warning messages. I was wondering if there was anyway around this. > The following warning messages I recieved are > warning return makes integer from pointer without a cast. > warning assignment makes pointer from integer without a cast. > a segment of same code that I have written is a follows : > openfile( char directory[] , char filename[], FILE *filep) > { >[snip] > { > FILE *filepointer, *fp; > char directory[50], filename[50]; > filepointer = (openfile(directory,filename,fp));
The prototype should be FILE *openfile( char directory[] , char filename[], FILE *filep) otherwise it is wrongly assumed that openfile returns an int (even though it says return filep; the return type is determined by the prototype, not by what gets returned). The program may still work, because pointers and ints can be of the same size on your computer. But on some other computers, they are not. Pedantic extras: - it's int main(void) - main should return a value, like return 0, before it ends - you don't need the ( ) around the openfile call. --
|
Tue, 08 Feb 2000 03:00:00 GMT |
|
 |
Kurt Watz #8 / 27
|
 const char* vs. char *
Quote:
>> (size_t). and under what situations should I opt for one over another? >size_t is really unsigned int.
size_t is an unsigned integral type, which is _not_ the same as an unsigned int. size_t can be an unsigned long int, or an unsigned char, if it can hold the size of objects that are supported by your implementation. Kurt -- | Kurt Watzka Phone : +49-89-2180-6254
|
Tue, 08 Feb 2000 03:00:00 GMT |
|
 |
George Sw #9 / 27
|
 const char* vs. char *
> ... I have a problem. > The following warning messages I recieved are > warning return makes integer from pointer without a cast. > warning assignment makes pointer from integer without a cast. > openfile( char directory[] , char filename[], FILE *filep) > { [snip...] > return(filep); > } You are returning a pointer here, but your function doesn't have an explicit type. The default function type is "int". You should explicitly declare this function to be a function returning "FILE *". > main () > { > FILE *filepointer, *fp; > char directory[50], filename[50]; > > filepointer = (openfile(directory,filename,fp)); This is your other line generating a warning. The compiler thinks openfile returns an integer. But you are assigning it to a pointer.
|
Tue, 08 Feb 2000 03:00:00 GMT |
|
|
Page 1 of 2
|
[ 27 post ] |
|
Go to page:
[1]
[2] |
|