ΪʲôһʹÓÃmallocº¯Êý³ÌÐò¾Í»á×Ô¶¯Ìø³ö£¿
#include <stdio.h>#include <stdlib.h>
#define Status int
#define OK 1
#define STACK_INIT_SIZE 6
#define OVERFLOW 0
typedefstruct {
int*base;
int*top;
intstacksize;
}Stack;
Status InitStack(Stack *S) {
system("pause");
S->base = (int *)malloc(STACK_INIT_SIZE *
sizeof(int));
if (!S->base) {
printf("·ÖÅäÄÚ´æʧ°Ü"); exit(OVERFLOW);
}
S->top = S->base;
S->stacksize = STACK_INIT_SIZE;
return OK;
}
void main()
{
Stack *S=NULL;
InitStack(S);
}
×î½üƵƵÓöµ½Õâ¸öÎÊÌ⣬ºÜÊÇÄÖÐÄ£¬×÷Òµ¶¼ÎÞ·¨Íê³É¡£Ò»µ©³ÌÐòÖ´Ðе½mallocº¯Êý¹â±êÀ¶È¦¾Í»áת¼¸È¦£¬È»ºóºóÐø´úÂ붼±»ÎÞÊÓûÓÐÖ´ÐУ¬Õû¸öexeµÄºÚ¿òÖ±½ÓÏûʧµô£¬ÕæµÄ²»ÖªµÀΪɶ¡£ÎÒÔÚÏëÄѵÀÊÇ·ÖÅäÄÚ´æʧ°ÜÁË£¿¾ÍдÁËÉÏÃæµÄ´úÂë²âÊÔһϣ¬µ«ÊÇÒªÊÇ·ÖÅäÄÚ´æʧ°ÜÄÇifÀïµÄprintf×ܵÃÖ´Ðаɣ¡Ò²Ã»ÓÐÎÄ×ÖÊä³ö·ÖÅäÄÚ´æʧ°Ü£¬µ½ÁËmalloc¾ÍÖ±½ÓÌø³ö£¬ÕâÊÇÄÖÄÄÑùÂ¡¤¡¤¡¤¡¤¡¤ÄѵÀ´æÔÚ×ÅÎÒ²»ÖªµÀµÄÓï·¨´íÎó£¿ º¦ÎÒÒÔΪ×Ô¼º¸ã´íÁË¡¡²»ÊÇÖ¸Õë~~#include <stdio.h>
#include <stdlib.h>
#define Status int
#define OK 1
#define STACK_INIT_SIZE 6
#define OVERFLOW 0
typedef struct STACK // ¸øÉϽṹÌåÃû
{
int *base;
int *top;
int stacksize;
}Stack;
Status InitStack(Stack *s)
{
s->base = (int *)malloc(STACK_INIT_SIZE * sizeof(int)); //ÄãÕâÀïʲôÊÂÇ飿£¿
if (!s->base) {
printf("Memory Allocation Failed!\n"); exit(OVERFLOW);
}
s->top = s->base;
s->stacksize = STACK_INIT_SIZE;
return OK;
}
int main() //Ç¿ÁÒ½¨ÒéÓà int main()
{
Stack s; //²»ÊÇÖ¸Õë
InitStack(&s);//ҪȡµØÖ·
return 0;
} claws0n ·¢±íÓÚ 2018-10-15 20:14
º¦ÎÒÒÔΪ×Ô¼º¸ã´íÁË¡¡²»ÊÇÖ¸Õë~~
ÕâÑù¸ÄȷʵÄÜÔËÐÐÁ˵«ÊÇ£¬Ö¸Õë±¾Öʲ»Ò²ÊǵØÖ·Âð£¬ÎªÉ¶ÓÃÖ¸Õë¾Í»á³ÌÐòÌø³ö¡¤¡¤¡¤¡¤¡¤¡¤ ÎÞµÐÌ«µ¶ ·¢±íÓÚ 2018-10-15 20:51
ÕâÑù¸ÄȷʵÄÜÔËÐÐÁ˵«ÊÇ£¬Ö¸Õë±¾Öʲ»Ò²ÊǵØÖ·Âð£¬ÎªÉ¶ÓÃÖ¸Õë¾Í»á³ÌÐòÌø³ö¡¤¡¤¡¤¡¤¡¤¡¤
Ö¸ÕëÖ»ÄÜ´æ´¢Ò»¸öµØÖ·£¬Ò²¾ÍÒ»Ñù¶«Î÷¶øÒÑ¡£
ÄãÒªÓÃÖ¸ÕëÒ²¿ÉÒÔ£¬µ«ÊǵÃÏȶÔÄãµÄÖ¸Õ뿪½á¹¹ÌåµÄ¿Õ¼ä£¬ÔÙÓöþ¼¶Ö¸Õ뿪ջµÄ¿Õ¼ä~~ºÎ¿à£¿
Ò³:
[1]