LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-18-2012, 01:10 PM   #1
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Rep: Reputation: 30
assignment makes pointer from integer without a cast [enabled by default]


Hi all, I've a warning message after compile time.
The warning is:

Code:
file.c: In function ‘main’:
file.c:16:10: warning: assignment makes pointer from integer without a cast [enabled by default]
My code is the following:

Code:
void main() {

  FILE *fp;

  typedef char STRING[20];

  struct s_type {
    STRING nome;
    STRING cognome;
  } utente;

  if (fp = fopen("test", "w")  == NULL) {
    printf("Impossibile aprire il file\n");
    exit(1);
  }

}
Where is the problem?
 
Old 09-18-2012, 01:27 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The problem is that you did not include the header required to declare fopen.

C lets you use external functions without declaring them. But when you do so, the compiler makes assumptions about those functions, which may be incorrect.

Depending on architecture details, those incorrect assumptions may cause compile time warnings or compile time errors or run time errors.

In this case, the error would be a serious run time error on an architecture (such as 64 bit Linux) in which pointers are a different size than integers. But is just on warning on 32 bit Linux or other architectures where a pointer and integer are more compatible.

If you type
man fopen
it should give you info including the fact that you need this include when using fopen
Code:
#include <stdio.h>

Last edited by johnsfine; 09-18-2012 at 01:31 PM.
 
Old 09-18-2012, 01:42 PM   #3
King_DuckZ
Member
 
Registered: Nov 2009
Location: Rome, IT
Distribution: Sabayon
Posts: 61

Rep: Reputation: 2
Allora, you don't mix Italian and C, it's so unelegant!
So let's see, the problem is that comparison has precedence over assignment (http://www.swansontec.com/sopc.html), so what this line is doing?
Code:
fp = fopen("test", "w")  == NULL
It first evaluates the comparison between fopen's return value and NULL, which yields an integer (1 or 0 arguably), then it says:
Code:
fp = 1
And that's your warning, il tuo puntatore è sminchio a bestia it means! That's the int being coerced into a pointer... try to read from fp and you should experience a beautiful crash! Mamma mia!!
Use parentheses to set the correct precedence:
Code:
if ((fp = fopen("test", "w")) == NULL) {

Last edited by King_DuckZ; 09-18-2012 at 01:50 PM.
 
Old 09-18-2012, 01:48 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by King_DuckZ View Post
the problem is that comparison has precedence over assignment
I guess I didn't read the first post carefully enough.

I agree with the above answer. (You still should include the right header to declare fopen, but the above issue is more serious).
 
Old 09-18-2012, 02:20 PM   #5
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
Thank you very much King :-))
 
Old 09-19-2012, 02:41 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Note: use these flags at compilation: -W -Wall -Wextra -pedantic -Werror
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
assignment makes integer from pointer without a cast kaskavi Programming 7 09-03-2011 12:07 AM
C - initialization makes integer from pointer without a cast... trist007 Linux - Newbie 1 05-15-2009 01:14 AM
"warning: assignment makes integer from pointer without a cast" in C christyyim Programming 4 12-23-2008 11:42 AM
makes pointer from integer without a cast ? hubabuba Programming 2 01-28-2005 05:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration