簡體   English   中英

指針數組的大小

[英]size of array of pointers

我對 sizeof 運算符有疑問

代碼 1:

int main()
{
    int p[10];
    printf("%d",sizeof(p));   //output -- 40
    return 0;
}

代碼 2:

int main()
{
    int *p[10];
    printf("%d",sizeof(*p));   //output -- 4
    return 0;
}

在第一個代碼中 p 指向一個整數數組。 在第二個代碼中 p 指向一個指針數組。 我無法理解為什么第一個代碼 o/p 是 40 但第二個代碼 o/p 是 4 認為兩者都指向相同大小的數組?

以下程序的輸出將為您提供有關類型大小和指向類型的指針的一些提示和理解。

#include <stdio.h>

int main(void)
{
    int p1[10];
    int *p2[10];
    int (*p3)[10];

    printf("sizeof(int)   = %d\n", (int)sizeof(int));
    printf("sizeof(int *) = %d\n", (int)sizeof(int *));
    printf("sizeof(p1)    = %d\n", (int)sizeof(p1));
    printf("sizeof(p2)    = %d\n", (int)sizeof(p2));
    printf("sizeof(p3)    = %d\n", (int)sizeof(p3));

    return 0;
}

int p[10];      => 10 consecutive memory blocks (each can store data of type int) are allocated and named as p

int *p[10];     => 10 consecutive memory blocks (each can store data of type int *) are allocated and named as p

int (*p)[10];   => p is a pointer to an array of 10 consecutive memory blocks (each can store data of type int) 

現在來回答你的問題:

>> in the first code p points to an array of ints.
>> in the second code p points to an array of pointers.

你是對的。 代碼中:2、要獲取p指向的數組的大小,需要傳入基地址

printf("%d", (int)sizeof(p));

而不是以下

printf("%d", (int)sizeof(*p));   //output -- 4

以下是等效的:

*p, *(p+0), *(0+p), p[0]

>> what's the difference between p[10] and 
>> (*p)[10]...they appear same to me...plz explain

以下是您另一個問題的答案:

int p[10];
 _________________________________________
|   0   |   1   |   2   |         |   9   |
| (int) | (int) | (int) |  ...    | (int) |
|_______|_______|_______|_________|_______|


int (*p)[10]
 _____________________
|                     |
| pointer to array of |
|     10 integers     |
|_____________________|

基本上,你有一個指針數組。 當您執行*p ,您取消了指向數組第一個元素的指針的引用。 因此,類型將是 int。 sizeof(int*)在你的機器上恰好是 4。

編輯(澄清):

代碼片段 1,您正在獲取數組的大小。

代碼片段 2,您正在獲取指針數組的第一個元素所指向的類型的大小。

在第一個代碼中:

int p[10];

p不指向整數數組; p一個整數數組。 在聲明中

printf("%d",sizeof(p));

這應該是

printf("%d", (int)sizeof(p));

表達式p仍然指的是數組對象,因此sizeof(p)產生數組對象的大小,在您的系統上恰好是 40 個字節( 10 * sizeof (int) )。

在第二:

int *p[10];

同樣, p一個指針數組。 但在以下聲明中:

printf("%d", (int)sizeof(*p));

表達式p被轉換為指向數組第一個元素(而不是整個數組)的指針。 取消引用該指針(使用一元*運算符)會為您提供一個int*對象,即數組的第一個元素。 系統上int*指針的大小恰好是 4。( sizeof (int)sizeof (int*)不一定相同;碰巧它們在您的系統上。)

在 C 中,規則是任何數組類型的表達式(例如數組變量的名稱)都會自動轉換為指向數組第一個元素的指針——大多數情況下 有以下三種例外情況:

  • 當它是sizeof的操作數時( sizeof arr產生數組對象的大小,而不是指針的大小)
  • 當它是一元&的操作數時( &arr產生整個數組對象的地址,而不是它的第一個元素的地址;相同的內存位置,不同的類型)
  • 當它是用於初始化數組對象的初始化程序中的字符串文字時( int arr[6] = "hello";不會將"hello"轉換為指針,而是復制數組)。

強烈推薦閱讀: comp.lang.c FAQ 的第 6 節。

檢查此代碼:

在這段代碼中 p 指向一個指針數組,因此 o/p 是 40

在您的情況下,它是一個指針數組,因此 o/p 是 4

#include<stdio.h>
int main()
{
       int (*p)[10];
       printf("%d",sizeof(*p));   //output -- 40
       return 0;
}

在第一個代碼中,sizeof(Array) 將返回為該數組分配的總字節數。 您可以通過以下方式獲得正確的數組大小

int size= sizeof(Array)/sizeof(Array[0]);

在第二個代碼中,它返回指針的大小。

看看你的第一個代碼片段 p 是大小為 40 字節的數組。現在它是 40 字節的結果很簡單,數組 p 確實包含 10 個成員,每個成員的大小為 4 個字節

   10*4=40

在第二個代碼片段中,p 只是它的指針數組,這意味着這個數組的每個成員都是一個指向 int 值的指針。現在 *p 表示數組第一個下標處的值,這個值只是一個 4 字節的地址。

希望事情現在對你來說很清楚。

暫無
暫無

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

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