English 中文(简体)
缩略语
原标题:Assigning a string to a variable of type int
  • 时间:2011-11-21 05:36:37
  •  标签:
  • c

Why is it that I can assign a string to a variable of type int? Eg. the following code compiles correctly:

int main(int argv, char** argc){
    int a="Hello World";
    printf(a);
}

Also, the program doesn t compile when I assign a string to a variable of a different type, namely double and char.

我认为,实际上正在做的是,汇编者执行<代码>int* a = “Hello World”;,在我撰写double a=“Hello World”;时,按现在规定执行该编码。

这是否正确?

最佳回答

事实上,这种派任是一种限制,要求从任何符合规定的C执行中进行诊断(可能只是警告)。 语言标准没有界定该方案的行为。

http://www.un.org。 限制载于rel=“nofollow”>C99标准,其中描述了供作简单转让的特许歌剧。 旧的C90标准基本相同。 预先国家调查C(如1978年公布的K&R1号文件所述)确实允许这种特定形式的默示转换,但自1989年版本以来,这种转换是无效的。

如果汇编成册,可能发生的情况是:

int a="Hello World";

如果是这样的话,将予以对待。

int a = (int)"Hello World";

投射物具有点值,转换成暗中。 这种转换的含义是实施界定的;如果char*int的大小不同,则可能失去信息。

某些转换可以明确进行,例如不同算术类型之间的转换。 这也许不是。

您的编辑should对此表示不满。 降低警告水平,直至达到这一水平。 (让我们看你再次使用什么汇编者,我们可以告诉你如何这样做)

http://www.un.org。

<代码>f 呼吁:

printf(a);

在C90没有界定的行为,是一种限制行为,需要在C99进行诊断,因为你重新要求使用一种没有明显原型的理论功能。 如果您希望打上<条码>,请上<>条码>。 a

#include <stdio.h>

(In some circumstances, the compiler won t tell you abut this, but it s still incorrect.) And given a visible declaration, since printf s first parameter is of type char* and you re passing it an int.

如果贵国的汇编者没有抱怨

double a="Hello World";

你们应当有一个更好的汇编者。 这(可能)试图将点值转换为<条码>杜布莱,这根本就没有任何意义。

问题回答

<代码>Hello World>为一系列特性,以char

When you assign its value to an int a, you assign the address of the first character in the array to a. GCC is trying to be kind with you.

当你印刷时,它就座到<代码>a各点,并打印所有特性,直至其达到char

It will compile because (on a 32-bit system) int, int *, and char * all correspond to 32-bit registers -- but double is 64-bits and char is 8-bits.

When compiled, I get the following warnings:

[11:40pm][wlynch@wlynch /tmp] gcc -Wall foo.c -o foo
foo.c: In function ‘main’:
foo.c:4: warning: initialization makes integer from pointer without a cast
foo.c:5: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast
foo.c:5: warning: format not a string literal and no format arguments
foo.c:5: warning: format not a string literal and no format arguments
foo.c:6: warning: control reaches end of non-void function

如你从警告中可以看到的那样,“Hello World”是一个指点,正在自动转换成一个星体。

您颁发的法典并不总是正确操作。 某个点人有时比一个点大。 如果是的话,你可以找到一个拖网点,然后在你试图使用这一数值时,就会出现非常奇怪的错误。

它就海湾合作委员会和部族等汇编者发出警告。

warning: initialization makes integer from pointer without a cast [enabled by default]

缩略语"Hello World" is a const char *,因此,你正在指定一个点为int(即,将第一种特性的地址作为int数值)。 在我的编辑上,gcc 4.6.2和部族-mac-lion将座右铭分配到int,unsign long,或char,所有这些都会产生警告,而不是错误。

坦率地说,这不是依赖的行为。 更不用说,您的<代码>印本(a);也是一种危险使用





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签