【发布时间】:2016-04-20 01:58:51
【问题描述】:
我对这个 C 程序有疑问。我不明白为什么即使我使用malloc() 指令初始化了我的数组,但无论我传递给我的函数初始化的第二个参数是什么,我都有相同的大小(4 字节)。
#include <stdio.h>
#include <stdlib.h>
typedef struct stack{
int size;
int *tab;
int top;
} stack;
void initialize(stack* st, int sizeArray);
int pop(stack* stack);
void push(stack* stack, int number);
int main(){
int sized;
stack S;
stack* ptr_S = &S;
printf("Enter the size of your stack please: \n");
scanf("%d", &sized);
//We send the pointer of the stack to initialise
initialize(ptr_S, sized);
printf("%d\t%d", S.size, S.top);
//printf("\nThe size of the array is: %d\n", sizeof(S.tab)/sizeof(int));
printf("\nThe size of the array is: %d\n", sizeof(S.tab));
pop(ptr_S);
return 0;
}
void initialize(stack* st, int sizeArray){
st->size = sizeArray;
st->top = 0;
st->tab = (int*)malloc(sizeof(int) * sizeArray);
}
【问题讨论】:
-
数组大小为
sizeof(int) * sized;它有sized元素。