473,804 Members | 3,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

warning: passing arg 1 of `fopen' makes pointer from integer without a cast

5 New Member
Hello,

Here is a program I'm playing around with for fun in the process of learning C. The objective is to create a function
Expand|Select|Wrap|Line Numbers
  1. filesize()
and call it from within the
Expand|Select|Wrap|Line Numbers
  1. main()
section to retrieve the size in bytes of a file that I give it.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. long filesize(f)
  4. {
  5.         FILE *fp;
  6.         if((fp = fopen(f, "r")) == NULL)
  7.         {
  8.                 printf("unable to open %s\n", f);
  9.                 exit(1);
  10.         }
  11.         fseek(fp, 0, SEEK_END);
  12.         long filesize = ftell(fp);
  13.         rewind(fp);
  14.         fclose(fp);
  15.         return(filesize);
  16. }
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.         if(argc < 2)
  21.         {
  22.                 printf("usage: %s <logfile>\n", argv[0]);
  23.                 exit(1);
  24.         }
  25.         long fsize = filesize(argv[1]);
  26.  
  27.         FILE *fp;
  28.         if((fp = fopen(argv[1], "r")) == NULL)
  29.         {
  30.                 printf("unable to open %s\n", argv[1]);
  31.                 exit(1);
  32.         }
  33.         printf("filesize of %s: %d bytes\n", argv[1], fsize);
  34.  
  35.         fclose(fp);
  36.         return 0;
  37. }
  38.  
Everything compiles just fine except for one annoying little warning I keep getting in reference to the file handle I'm trying to open in the
Expand|Select|Wrap|Line Numbers
  1. filesize()
function. When I go to compile the program, it gives me this error:

Expand|Select|Wrap|Line Numbers
  1. log.c: In function `filesize':
  2. log.c:6: warning: passing arg 1 of `fopen' makes pointer from integer without a cast
I'm wondering if it's possible to eliminate that warning by making whatever change is necessary. The two lines that I'm aiming to make possible by declaring this particular function are as follows:

Expand|Select|Wrap|Line Numbers
  1. long fsize = filesize(argv[1]);
  2. printf("filesize of %s: %d bytes\n", argv[1], fsize);
  3.  
Any help would be appreciated! The program will still compile and run just fine, but it'd be nice to compile and not have to see that warning.

By the way, I'm only interested in programming in C at this time.
Mar 9 '07 #1
3 17220
mrmattborja
5 New Member
Ah, nevermind, I found it.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. long filesize(char f[])
  4. {
  5. ...
  6.  
Should've seen it before :-P
Mar 9 '07 #2
Ganon11
3,652 Recognized Expert Specialist
Good to see you got it on your own. :)
Mar 9 '07 #3
mrmattborja
5 New Member
Here's my new function...head er include even!

Expand|Select|Wrap|Line Numbers
  1. /* filesize.h - returns size in bytes of filename */
  2. long filesize(char f[])
  3. {
  4.         FILE *fp;
  5.         if((fp = fopen(f, "r")) == NULL)
  6.         {
  7.                 fprintf(stderr, "failed to open %s\n", f);
  8.                 exit(1);
  9.         }
  10.         fseek(fp, 0, SEEK_END);
  11.         long filesize = ftell(fp);
  12.         fclose(fp);
  13.         return(filesize);
  14. }
  15.  
Mar 9 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

27
4773
by: MK | last post by:
I am a newbie. Please help. The following warning is issued by gcc-3.2.2 compiler (pc Linux): ================================================================== read_raw_data.c:51: warning: assignment makes pointer from integer without a cast ================================================================== when the following piece of code was compiled. The offending statement is calloc. A similar statement in the main() function...
4
36187
by: Dawn Minnis | last post by:
Hi When I compile my files I get the following: driver.c: In function `main': driver.c:49: warning: assignment makes integer from pointer without a cast driver.c:50: warning: assignment makes integer from pointer without a cast driver.c:51: warning: assignment makes integer from pointer without a
16
5873
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program compiles fine. What is the warning shown ?
2
14363
by: francescomoi | last post by:
Hi. I'm trying to compile this piece of source: ------------------------------------------- int id; while(row1 = mysql_fetch_row(rs1)) { id = atoi((int)row1); -----------------------------------
29
2530
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
4
6571
by: metalinc | last post by:
hi...im new to C programming...need help...tried to run this code but got this error fork.c: In function ‘parse’: fork.c:44: warning: comparison between pointer and integer fork.c:51: warning: assignment makes integer from pointer without a cast fork.c:61: warning: comparison between pointer and integer /tmp/cciECfg4.o: In function `main': fork.c:(.text+0x3a): warning: the `gets' function is dangerous and should not be used. #include...
7
5292
by: llothar | last post by:
When i use -W4 on visual c 7.0 i get warning C4054 translator1.c(1703) : warning C4054: 'type cast' : from function pointer 'void * (__cdecl *)(se_agent *)' to data pointer 'void *' translator1.c(1703) : warning C4152: nonstandard extension, function/ data pointer conversion in expression whenever i cast a function pointer to a void* or back. Is there any reason for this warning ? Looks like good C code for me.
10
9506
drhowarddrfine
by: drhowarddrfine | last post by:
warning: assignment makes pointer from integer without a cast I get that when I compile a C file where the function is defined as this: char **getvars() and the calling function has this declared: char **s; and I write: s=getvars();
1
3836
by: woods1313drew | last post by:
The following code in c+ gives me the warning assignment makes integer from pointer without a cast. destination is set as char destination to limit the input string to 10 characters. name is an array of ten places char name iv looked in several books but cant find an references to integer pointer errors. Could someone tell me what i am doing wrong thanks. scanf("%s",destination); e = (strcmpi(destination,"exit")); ...
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9571
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2976
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.