簡體   English   中英

如何修復 function a3 以獲得我的 output

[英]how can i fix the function a3 to obtain my output

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char null;

static char a3[](char a[], int start, int length)
{
    if(length < 0 || start < 0 || start + length -1 >= sizeof(a))
    {
        return null;
    }

 char sub[] = new char[length];
 for (int i=start, j=0; j<length; i++, j++)
 {
 sub[j] = a[i];
 }

 return sub;
}



int main()
{
   char a[]= {'a', 'b','c'};
   int start = 0;
   int length = 3;
   printf("%d\n", a3(a, start,length));
    return 0;
}

我想寫一個 function 接受一個字符數組,一個從零開始的 position 和一個長度。 它應該返回一個字符數組,其中包含從輸入數組的起始字符開始的長度字符。 function 應該對開始 position 和長度進行錯誤檢查,如果任一值不合法,則返回 null。 當我構建它給我以下錯誤“錯誤:在'{'令牌|之前預期'=',',',';','asm'或'屬性'”。 我不知道如何解決這個錯誤。 誰能幫我??

數組類型的值不能傳遞給函數、從函數返回或分配給數組類型的 object。 但是,指向數組元素的指針可以傳遞給 function 或從 function 返回。 因此,function a3的原型應該是這樣的:

static char *a3(char a[], int start, int length)

數組類型的 function 參數自動調整為指針類型,並將指向數組的第一個元素。 因此上面的原型等價於:

static char *a3(char *a, int start, int length)

function 無法確定數組的大小,因為數組參數實際上只是一個指針。 function a3中的表達式sizeof(a)等價於sizeof(char *)並且與數組的長度無關。 function 將需要以其他方式告知數組的長度,也許還有另一個參數:

static char *a3(char *a, int a_len, int start, int length)

然后測試正確參數的代碼可以是這樣的:

    if (length < 0 || start < 0 || start + length > a_len)
    {
        return NULL;
    }

請注意, NULL是一個 null 指針常量,返回值將是一個char *類型的 null 指針值。 這比將變量null的值轉換為沒有強制轉換的char *的現有代碼更可取。

如果您在 function 或塊中創建具有自動存儲 class 的數組或其他變量,則在到達 function 或塊的末尾時它不再存在。 因此,如果 function 返回指向此類變量的指針,則該指針將無效。 調用者不能使用它來訪問變量的內容,因為它不再存在。 Therefore, the function should use the memory management functions such as malloc or calloc to allocate storage for the object and return a pointer to that storage. 存儲在該地址上持續存在,直到調用free以釋放存儲,或調用realloc以更改其大小。

現有代碼使用 C++ new運算符為數組sub創建存儲,但 C 不支持。 C 等效項是將sub聲明為指針並使用malloccalloc來分配存儲。 calloc還將返回的 memory 塊的所有字節設置為 0。):

    char *sub = malloc(length * sizeof(char));

或者:

    char *sub = calloc(length, sizeof(char));

注意sizeof(char)的定義是 1,所以上面可以簡化為:

    char *sub = malloc(length);

或者:

    char *sub = calloc(length, 1);

malloccalloc可能無法分配 memory(對於分配少量內存的小程序不太可能),在這種情況下它們返回 null 指針。 function 應該檢查並返回錯誤:

    if (sub == NULL)
    {
        return NULL;
    }

main中有一個錯誤,它使用printf %d說明符打印a3()的返回值。 %d說明符需要一個int類型的值(在默認參數 Promotions之后),所以我不知道 OP 期望在那里打印什么。

將它們放在一起並更改main的 function 以打印一些合理的東西,我們有以下內容:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char *a3(char a[], int a_len, int start, int length)
{
    if(length < 0 || start < 0 || start + length > a_len)
    {
        return NULL;
    }

    char *sub = malloc(length);
    if (sub == NULL)
    {
        return NULL;
    }
    for (int i=start, j=0; j<length; i++, j++)
    {
        sub[j] = a[i];
    }

    return sub;
}



int main(void)
{
    char a[]= {'a', 'b','c'};
    int a_len = 3;
    int start = 0;
    int length = 3;
    char *sub = a3(a, a_len, start, length);

    if (sub == NULL)
    {
        printf("Error\n");
        exit(EXIT_FAILURE);
    }
   
    for (int i = 0; i < length; i++)
    {
        printf("%c", sub[i]);
    }
    printf("\n");

    free(sub); // free the storage returned by a3()
    return 0;
}

可以進行各種改進。 例如,可以將a3a參數更改為const char *a (或const char a[] ,因為它將被調整為const char *a )以表明它不會修改a指向的內容。

暫無
暫無

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

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