ÓãCÂÛ̳

 ÕÒ»ØÃÜÂë
 Á¢¼´×¢²á
²é¿´: 1418|»Ø¸´: 3

[Òѽâ¾ö]ΪʲôһʹÓÃmallocº¯Êý³ÌÐò¾Í»á×Ô¶¯Ìø³ö£¿

[¸´ÖÆÁ´½Ó]
·¢±íÓÚ 2018-10-15 18:29:29 | ÏÔʾȫ²¿Â¥²ã |ÔĶÁģʽ

ÂíÉÏ×¢²á£¬½á½»¸ü¶àºÃÓÑ£¬ÏíÓøü¶à¹¦ÄÜ^_^

ÄúÐèÒª µÇ¼ ²Å¿ÉÒÔÏÂÔØ»ò²é¿´£¬Ã»ÓÐÕ˺ţ¿Á¢¼´×¢²á

x
#include <stdio.h>
#include <stdlib.h>
#define Status int
#define OK 1
#define STACK_INIT_SIZE 6
#define OVERFLOW 0
typedef  struct {
        int  *base;
        int  *top;
        int  stacksize;
}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¾ÍÖ±½ÓÌø³ö£¬ÕâÊÇÄÖÄÄÑùÂ¡¤¡¤¡¤¡¤¡¤ÄѵÀ´æÔÚ×ÅÎÒ²»ÖªµÀµÄÓï·¨´íÎó£¿
×î¼Ñ´ð°¸
2018-10-15 20:14:34
º¦ÎÒÒÔΪ×Ô¼º¸ã´íÁË¡­¡­²»ÊÇÖ¸Õë~~
#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;
}
ÏëÖªµÀС¼×Óã×î½üÔÚ×öɶ£¿Çë·ÃÎÊ -> ilovefishc.com
·¢±íÓÚ 2018-10-15 20:14:34 | ÏÔʾȫ²¿Â¥²ã    ±¾Â¥Îª×î¼Ñ´ð°¸   
º¦ÎÒÒÔΪ×Ô¼º¸ã´íÁË¡­¡­²»ÊÇÖ¸Õë~~
#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;
}
ÏëÖªµÀС¼×Óã×î½üÔÚ×öɶ£¿Çë·ÃÎÊ -> ilovefishc.com
 Â¥Ö÷| ·¢±íÓÚ 2018-10-15 20:51:09 | ÏÔʾȫ²¿Â¥²ã
claws0n ·¢±íÓÚ 2018-10-15 20:14
º¦ÎÒÒÔΪ×Ô¼º¸ã´íÁË¡­¡­²»ÊÇÖ¸Õë~~

ÕâÑù¸ÄȷʵÄÜÔËÐÐÁ˵«ÊÇ£¬Ö¸Õë±¾Öʲ»Ò²ÊǵØÖ·Âð£¬ÎªÉ¶ÓÃÖ¸Õë¾Í»á³ÌÐòÌø³ö¡¤¡¤¡¤¡¤¡¤¡¤
ÏëÖªµÀС¼×Óã×î½üÔÚ×öɶ£¿Çë·ÃÎÊ -> ilovefishc.com
·¢±íÓÚ 2018-10-15 20:55:45 | ÏÔʾȫ²¿Â¥²ã
ÎÞµÐÌ«µ¶ ·¢±íÓÚ 2018-10-15 20:51
ÕâÑù¸ÄȷʵÄÜÔËÐÐÁ˵«ÊÇ£¬Ö¸Õë±¾Öʲ»Ò²ÊǵØÖ·Âð£¬ÎªÉ¶ÓÃÖ¸Õë¾Í»á³ÌÐòÌø³ö¡¤¡¤¡¤¡¤¡¤¡¤

Ö¸ÕëÖ»ÄÜ´æ´¢Ò»¸öµØÖ·£¬Ò²¾ÍÒ»Ñù¶«Î÷¶øÒÑ¡£
ÄãÒªÓÃÖ¸ÕëÒ²¿ÉÒÔ£¬µ«ÊǵÃÏȶÔÄãµÄÖ¸Õ뿪½á¹¹ÌåµÄ¿Õ¼ä£¬ÔÙÓöþ¼¶Ö¸Õ뿪ջµÄ¿Õ¼ä~~ºÎ¿à£¿
ÏëÖªµÀС¼×Óã×î½üÔÚ×öɶ£¿Çë·ÃÎÊ -> ilovefishc.com
ÄúÐèÒªµÇ¼ºó²Å¿ÉÒÔ»ØÌû µÇ¼ | Á¢¼´×¢²á

±¾°æ»ý·Ö¹æÔò

СºÚÎÝ|ÊÖ»ú°æ|Archiver|ÓãC¹¤×÷ÊÒ ( ÔÁICP±¸18085999ºÅ-1 | ÔÁ¹«Íø°²±¸ 44051102000585ºÅ)

GMT+8, 2024-11-30 13:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

¿ìËٻظ´ ·µ»Ø¶¥²¿ ·µ»ØÁбí