2013-10-12 138 views
-3
#include <stdio.h> 
#include <stdlib.h> 
int main() 
{ 
    int a=2,*p; 
    p=&a; 
    printf ("address of a=%u", p); 
    return (0); 
} 

當我在Ubuntu執行該代碼將打印此錯誤:當我執行這段代碼時爲什麼發生錯誤?

format '%u' expects argument of type 'unsigned int', 
but argument 2 has type 'int *' [-wformat]. 

爲什麼即使在從一本書代碼這種類型的錯誤?我知道這是一個愚蠢的問題,但我很困惑。

+1

當你執行?或者當你編譯?這是一個錯誤還是一個警告? – thang

+0

當我編譯和它的警告 – user2873459

+1

也許這就是你應該在帖子中,而不是說「當我執行」和「打印這個錯誤」。 – thang

回答

4

when I execute this code on ubuntu it will print this error:

Naw。它打印時,你編譯它。

而這是因爲%u is not suited for printing pointers。爲此目的使用%pAnd do read the documentation

+0

那麼爲什麼它在turbo c上執行? – user2873459

+2

@ user2873459因爲Turbo C是一個蹩腳的,過時的,愚蠢的編譯器,它不知道格式字符串錯誤的任何信息。 – 2013-10-12 07:58:45

+0

ohk當我減去兩個地址使用像指針 int * I,* j,a [] = {1,2,3,4}; I = &a[0]; j = &a[2]; printf(「%p」,j-I); 爲什麼它會給錯誤 – user2873459

相關問題