簡體   English   中英

當聲明一個作為數組的雙指針時,為什么不需要放方括號,因為它是一個指針數組?

[英]When Declaring a Double Pointer that is an array why is there no need to put brackets because it is an array of pointers?

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** AllocateShoppingList(int numFoods);
char* AllocateItem();
int DetermineNumberOfCandy(char** list, int numFoods);

int main()
{
  
   char** stringArray;// pointer to pointers
   char* words; //pointer to char array
   //1. ask the user how many foods are on their list
   int foods;
   printf("How many foods on the shopping list?\n");
   scanf("%d", &foods);
   //2. call the AllocateShoppingList function and store the result
   stringArray = AllocateShoppingList(foods);   
   //3. for X times (user's previous input), call the AllocateItem function 
   //       and store the result in the list of pointers (step 2)
   for(int i =0;i < foods; i++){
   words = AllocateItem();
   stringArray[i] = words;}

  //strcpy(stringArray[i],words); why not work?
   
   //4. call the DetermineNumberOfCandy function, and print the result
   printf("Candy appeared this many times: %d\n",DetermineNumberOfCandy(stringArray, foods)); 
   //5. free up all of the pointers that are held by the shopping list
   //6. free up the shopping pointer itself
   free(words);
   free(stringArray);
   
}

int DetermineNumberOfCandy(char** list, int numFoods)
{
   //0. setup a counter variable
   int counter = 0 ;
   //1. for each pointer in the shopping list:
   //1a.   compare the string at the pointer's address to "candy"
   
   for(int i =0; i< numFoods; i++)
   if (strcmp(list[i],"candy")==0)
     // why you dont have to dereference it
     // this concept works with single pointers
     // does it work with double or tripple pointers as long as it orignally points to the string?
     
   counter++;
   
   //1b.   if it is candy, then tick up the counter variable
   //2. return the counter variable
   return counter;
}

char** AllocateShoppingList(int numFoods)
{
   
   return calloc(numFoods, sizeof(char*));
   //1. allocate memory that can hold X pointers (X = numFoods)
   //2. return the memory address holding this pointer to pointers
}

char* AllocateItem()
{
   char* wordPtr;
   char word[100];
   //1. get a word from the user that is max 100 characters
 
   scanf("%s", word);
   //2. determine how large the word actually is
   wordPtr = calloc(strlen(word)+1,sizeof(char));
   strcpy(wordPtr, word);
   //3. allocate memory that is just enough to hold the word
   return wordPtr;
   //4. copy the user's word into the new memory location
   //5. return the memory address holding the word
}

**對於這段代碼,我們必須最后一次購物並查看打印出糖果在購物清單中的次數,為了做到這一點,我們必須分配足夠的 memory 來保留用戶想要的盡可能多的單詞(字符串)列入他們的購物清單。 然后,對於每個項目,分配剛好足夠的 memory 來存儲單詞。 **

這個聲明對我來說沒有意義

**char** stringArray; **

據我了解,雙指針是一個數組,數組中的元素包含指向字符串地址的指針。

因此,我認為我們必須像這樣聲明雙指針:

字符stringArray[]; **

像這樣的東西,但那是行不通的。

所以我想知道如果我們從不放括號,代碼如何知道它是一個指針數組

我嘗試用一個數組聲明雙指針,但無法讓它工作,也無法弄清楚它是否可能。

當你像這樣聲明一個像這樣的字符串的變量時: char str[10]; 您的計算機實際上聲明了一個指針,但分配了足夠的 memory 來自動保存 10 個字符。 您會看到,如果取消引用它,您將獲得字符串的第一個字符。

關於你的 strcpy 在第 20 行不工作,if 不工作是因為你在 for 循環中創建了你的 i 變量,當你這樣做時,變量在循環結束時消失,所以你無法在你的代碼中使用它。

關於第 40 行,您可以至少以兩種不同的方式使用指針。 第一個是在 function 中傳遞一個變量作為指針,這樣您就不必像這樣在 function 的末尾返回它:

int main()
{
    int var = 0;
    my_funct(&var);
    //var = 1 now
}

void myfunct(int *var)
{
    *var = 1;
}

在這里您需要取消引用它,但是如果您將 memory 分配給您的指針,您現在可以將它用作數組而無需取消引用它。

哦,在這里,您只需釋放 stringarray 中的 2 個指針中的一個。 要釋放一切,請嘗試:

for(int i = 0; i < foods; i++) {
    free(StringArray[i]);
}
free(StringArray);

告訴我是不是我對所有事情都不了解,或者我不夠清楚

指針是指針,arrays 是 arrays。但是,當 C 中的數組用於表達式或作為參數傳遞給 function 時,它“衰減”為指向該數組第一個元素的指針。 這反過來又使指針算法和[]索引運算符的方便使用成為可能。

這也意味着在大多數情況下,可以使用指向第一個元素的指針來代替數組。 如果我們有一個指針數組char* arr[n]; 然后可以使用char**指向第一項,並從那里指向數組的 rest。

所以如果你寫一個 function 像int DetermineNumberOfCandy(int numFoods, char* list[numFoods]); 這很好並且有效 C,但是無論如何將“衰減” list指向第一個元素的指針,因此它 100% 等同於
int DetermineNumberOfCandy(int numFoods, char** list);


您的代碼中還有其他錯誤。

  • 由於AllocateItem分配了一個有效的 memory 位置,因此必須先將 main() 中的stringArray[i]分配給這個 memory 位置,然后才能使用它。 因為在那之前它只是一個指向垃圾的未初始化指針。 因此你不能strcpy(stringArray[i], words) 請記住,function 不僅分配了 memory 的一塊,而且還用有效數據填充了它。 所以將指針設置為指向該數據就足夠了,不需要復制任何東西。

    變量這個word沒有任何意義,你也可以這樣寫:

     for(int i=0; i < foods; i++){ stringArray[i] = AllocateItem(); }
  • 同樣free(words)是錯誤的,這只會釋放最后分配的 memory。經驗法則:對於每個malloc調用,您必須有一個相應的free調用:因此它應該是:

     for(int i=0; i < foods; i++){ free(stringArray[i]); } free(stringArray);

暫無
暫無

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

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