簡體   English   中英

為什么下面的代碼不打印值10?

[英]Why the below code is not printing the value 10?

為什么下面的代碼不打印值10? 而是崩潰了..

void foobar(int *a) 
{   
    int *c;
    c = *a;

    printf("%d\n", *c);
}

int main(void)
{   
    int *b = (int *)malloc(sizeof(int));
    *b = 10;
    foobar(&b);
}   

嘗試進行這些小的更改。

#include <stdio.h>
#include <malloc.h>

void foobar(int *a) {
  printf("%d\n", *a); /* what was C about? */
}

int main(void) {
  int *b = malloc(sizeof(int));
  *b = 10;
  foobar(b); /* pass the pointer b, not the address of the pointer b. */
  free(b);
}

首先,您應該包括stdio.hstdlib.h

其次,您的函數聲明不正確。 它應該是

void foobar(int **a)

代替

void foobar(int *a)  

因為您將指針傳遞給指針。

該代碼未包含<stdlib.h>並且大多數likley在64位上運行。

要解決此問題,請執行以下操作:

#include <stdlib.h>  /* for prototype of malloc() */
#include <stdio.h>  /* for prototype of printf() */

void foobar(int ** a)  /* Fixed wrong type. */
{   
  int * c = *a;

  printf("%d\n", *c);
}

int main(void)
{    
  int * b = malloc(sizeof(*b)); /* Removed unnecessary and dangerous cast. */
  if (NULL != b) /* Added error checking. */
  {
    *b = 10;

    foobar(&b);
  }

  free(b); /* Added clean up. */

  return 0; /* Added return (unnecessary from C99 on). */
}  
#include <stdio.h>
#include<stdlib.h> //--------> ERROR 3: INCLUDE STDLIB FOR MALLOC() FUNCTION

 void foobar(int *a)
{
    int *c;
    c = *a; //--------->ERROR 2 : C POINTS TO THE CONTENTS OF A POINTER

    printf("%d\n", *c);
}

 // SOLUTION 1 : KEEP FUNCTION PROTOTYPE SAME AND PASS B TO FOOBAR AND MAKE C POINT TO    A.
 // SOLUTION 2 : CHANGE FOOBAR'S PROTOTYPE TO FOOBAR(INT**A).

 int main(void)
{
     int *b = (int *)malloc(sizeof(int));
     *b = 10;
     foobar(b); //-------->ERROR 1 : PASSING THE ADDRESS OD A POINTER VARIABLE

}

解決方案1:foobar(b); 在main和c = a;

解決方案2:foobar(int ** a);

由於無效的foobar(int * a)中的參數類型錯誤而崩潰

它應該是無效的foobar(int ** a)

雖然對於要求只是正確的foobar(&b)=> foobar(b)

我們先討論一下類型。

給出聲明

T *p;

那么以下所有內容都是正確的:

Expression         Type        Value
----------         ----        -----
         p         T *         Address of the thing p points to
        *p         T           Value of the thing p points to
        &p         T **        Address of the variable p

main ,你聲明bint * foobar ,將ac都聲明為int *類型。

在對foobar的調用中,您使用的表達式&b類型為int ** 這不符合的類型匹配a (而且應該從編譯器產生了一個診斷)。 您正在做的就是將變量 b的地址傳遞給foobar ,這不是您想要的。 您想要傳遞b包含的值, b是從malloc調用中獲取的內存的地址。 因此,如果您foobar的調用更改為讀取

foobar(b);

該代碼應按預期工作。

圖片可能會有所幫助。 所有地址都是憑空冒出來的,並不代表任何實際平台。 還假定使用大端序表示法:

Item        Address        0x00  0x01  0x02  0x03
----        -------        ----  ----  ----  ----
heap memory 0x00800000     0x00  0x00  0x00  0x0a
   b        0xfff78000     0x00  0x80  0x00  0x00
   a        0xfff781c0     0x00  0x80  0x00  0x00
   c        0xfff781c4     0x00  0x80  0x00  0x00

malloc的調用從地址0x008000000開始分配4個字節; 在分配*b = 10 ,這四個字節包含值0x0000000a。

bac都是具有auto范圍的變量; 這三個都包含堆內存地址作為它們的值。 *b*a*c 表達式都應求值為存儲在地址0x008000000上的值; 即,整數值10 &b的結果為b的地址,即0xfff78000。

正如其他人提到的,不要malloc的結果。 它不是必需的(至少,從1989年的標准開始,不是C; C ++和早期版本的C是不同的故事),並且在1989年的標准下,如果您忘記了#include stdio.h或以其他方式不這樣做,它可以抑制有用的診斷。在范圍內沒有malloc的聲明。 malloc調用的一般形式是

T *p = malloc( sizeof *p * num_elements );

所以在你的情況下它會讀

int *b = malloc( sizeof *b );

請注意,即使是像這樣的玩具程序,也應該始終自己清理一下,因此您需要添加

free( b );

在退出程序之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM