sizeof东西和声明 [英] sizeof stuff and declarations
问题描述
我有两个问题。我希望他们是关于C.我选择在这里发布
而不是comp.unix.programmer(或其他一些),因为我这样做。
认为这是一个C问题。
问题1:
%cat float.c
main(){
float ** m = malloc(5 * sizeof(float *));
m [0] = malloc(sizeof(float));
m [0] [ 0] = 1.2;
printf("%f\ n,m [0] [0]);
}
这是合法的吗?我从来没有真正宣布和分配如此之快,但我想知道它是否有效,因为我很幸运,或者它真的是
合法。 Gcc不喜欢它。
%gcc -c float.c
float.c:在函数`main'':
float.c:2:警告:初始化使得整数指针
没有强制转换浮点数:3:警告:赋值指针来自
整数没有演员
问题2:
(gdb)p sizeof(int)
$ 1 = 4
(gdb)p sizeof(长)
$ 2 = 4
我期待看到更长一点。
为什么它们大小相同?
谢谢。
I have two questions. I hope they are about C. I chose to post
here instead of comp.unix.programmer (or some other) because I do
think this is a C question.
Question 1:
%cat float.c
main() {
float **m = malloc(5 * sizeof(float*));
m[0] = malloc( sizeof(float) );
m[0][0] = 1.2;
printf("%f\n",m[0][0]);
}
Is this legal? I never really declare and assign so fast, but I
was wondering if it works because I''m lucky or if it really is
legal. Gcc doesn''t like it much.
%gcc -c float.c
float.c: In function `main'':
float.c:2: warning: initialization makes pointer from integer
without a cast float.c:3: warning: assignment makes pointer from
integer without a cast
Question 2:
(gdb) p sizeof(int)
$1 = 4
(gdb) p sizeof(long)
$2 = 4
I was expecting to see long a little bigger.
Why are they the same size?
Thank you.
推荐答案
1 = 4
(gdb)p sizeof(长)
1 = 4
(gdb) p sizeof(long)
2 = 4
我当时期待看到更长一点。
为什么它们大小相同?
谢谢。
2 = 4
I was expecting to see long a little bigger.
Why are they the same size?
Thank you.
Kevin写道:
我有两个问题。我希望他们是关于C.我选择在这里发布
而不是comp.unix.programmer(或其他),因为我确实认为这是一个C问题。
问题1:
%cat float.c
main(){
float ** m = malloc(5 * sizeof(float *));
m [ 0] = malloc(sizeof(float));
m [0] [0] = 1.2;
printf("%f\ n,m [0] [0]);
}
这是合法的吗?我从来没有真正宣布和分配这么快,但我想知道它是否有效,因为我很幸运,或者它真的是合法的。 Gcc不太喜欢。
%gcc -c float.c
float.c:在函数`main''中:
float.c:2:警告:初始化使得指针来自整数
而没有强制转换float.c:3:警告:赋值使得指针来自
整数而没有强制转换
你忘了做声明' 'malloc''的功能。这就是导致
警告的原因,因为编译器假定''malloc''返回''int''。
放置''#include< stdlib.h>''在源文件的开头和
警告将消失。
您也忘了声明''printf''功能。 ''printf''是一个可变的
函数。它在使用前声明,或行为未定义。
在源文件的开头添加''#include< stdio.h>''。
否则,代码是合法的。
问题2:
(gdb)p sizeof(int)
I have two questions. I hope they are about C. I chose to post
here instead of comp.unix.programmer (or some other) because I do
think this is a C question.
Question 1:
%cat float.c
main() {
float **m = malloc(5 * sizeof(float*));
m[0] = malloc( sizeof(float) );
m[0][0] = 1.2;
printf("%f\n",m[0][0]);
}
Is this legal? I never really declare and assign so fast, but I
was wondering if it works because I''m lucky or if it really is
legal. Gcc doesn''t like it much.
%gcc -c float.c
float.c: In function `main'':
float.c:2: warning: initialization makes pointer from integer
without a cast float.c:3: warning: assignment makes pointer from
integer without a cast
You forgot do declare ''malloc'' functions. That''s what causes the
warning, since the compiler assumes that ''malloc'' returns an ''int''.
Place ''#include <stdlib.h>'' at the beginning of your source file and the
warning will go away.
You also forgot to declare ''printf'' function. ''printf'' is a variadic
function. It mast be declared before use, or the behavior is undefined.
Add ''#include <stdio.h>'' at the beginning of your source file.
Otherwise, the code is legal.
Question 2:
(gdb) p sizeof(int)
这篇关于sizeof东西和声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!