需要一些幫助來分解這個 C 函式。我被困在 malloc 線上。我不明白“ 8”在做什么和/或為什么它甚至在那里。我的研究表明它與地址算術有關。此外,我不介意幫助檢查每行代碼的其余細分(注釋)是否正確。
// Prints help message to the console
// Returns a string
char * helloWorld(char * name) { //The * is a pointer to an address. (char * name) = the address to a char pointer stored/passed via name.
char * answer = malloc(strlen(name) 8); //syntax = (cast-type*) malloc(byte-size); answer has a * because malloc returns a void pointer. The * deferences the pointer
//and tells the program to use the value at that member address.
//strlen returns a
printf("This prints to the console when you Run Tests");
strcpy(answer, "Hello, "); //This returns a pointer to the destination string dest. char *strcpy(char *dest, const char *src)
//(answer {dest}, "Hello, "{string to be copied to dest}) *dest= The value at the pointer of src("Hello, ") will be assigned.
strcat(answer {des}, name {src}); //char *strcat(char *dest, const char *src) --> appends the string pointed to by src to the end of the string pointed to by dest.
return answer; //adds name to the end of answer. -->Hello, name.
}
uj5u.com熱心網友回復:
它與地址算術完全無關。strlen(name) 8就是這樣;取長度name并加8。
8 是由字串長度Hello, (7 個位元組)加上結果字串的空終止符的 1 個位元組組成的幻數。
uj5u.com熱心網友回復:
我的研究表明它與地址算術有關。
那是錯的。這里不涉及地址算術。
您沒有告訴我們您希望看到什么,而不是 8.
如果您strlen(name)直接使用,則沒有終止 0 位元組的空間。
而且只有復制name到新記憶體的空間。但是您想"Hello, "在記憶體區域的開頭添加。該字串占用 7 個位元組。
最后,7 個位元組"Hello, "和 1 個位元組用于以 0 位元組結束字串,總共需要在分配記憶體時添加 8 個位元組。
uj5u.com熱心網友回復:
這里沒有指標運算。該malloc函式被告知分配strlen(name) 8記憶體位元組,即足夠name加上額外的 7 個字符和一個空終止位元組。
以下行將字串復制"Hello, "到answer然后連接name到它。前導字串"Hello, "有 7 個字符,所以這就是分配中額外的 7 的用途。
uj5u.com熱心網友回復:
如果您寫信回答,則必須確保它有足夠的空間來容納整個結果字串。因為這個大小只在運行時才需要,所以分配了空間。(第二個原因是你回傳了它的地址,所以你不能把它放在堆疊上)。
如果您查看看到的代碼,答案將是Hello, (length = 7) 加上名稱 (length = strlen(name)) 加上終止的空位元組 (length = 1)。所以strlen(name) 8將完全符合答案。
uj5u.com熱心網友回復:
它需要足夠的記憶體用于輸入字串( 的結果strlen)、一個前綴"Hello, "(7 個字符)和一個終止 NUL 字符(1 個字符),因此將 8 添加到strlen以獲得分配所需的總記憶體。
正如您所指出的,這有點令人困惑(幻數通常是),并且脆弱(對前綴的每次更改都需要對看似不相關的幻數進行匹配更改),因此在更好的代碼中您可能會執行以下操作:
// Prints help message to the console
// Returns a string
char * helloWorld(char * name) { //The * is a pointer to an address. (char * name) = the address to a char pointer stored/passed via name.
static const char PREFIX[] = "Hello, ";
// Conveniently, the size of the array includes the NUL too
char * answer = malloc(strlen(name) sizeof(PREFIX)); //syntax = (cast-type*) malloc(byte-size); answer has a * because malloc returns a void pointer. The * deferences the pointer
//and tells the program to use the value at that member address.
//strlen returns a
printf("This prints to the console when you Run Tests");
strcpy(answer, PREFIX); //This returns a pointer to the destination string dest. char *strcpy(char *dest, const char *src)
//(answer {dest}, "Hello, "{string to be copied to dest}) *dest= The value at the pointer of src("Hello, ") will be assigned.
strcat(answer {des}, name {src}); //char *strcat(char *dest, const char *src) --> appends the string pointed to by src to the end of the string pointed to by dest.
return answer; //adds name to the end of answer. -->Hello, name.
}
這清楚地說明了為什么您要增加分配的長度而沒有與前綴本身直接相關的幻數。
uj5u.com熱心網友回復:
8正如其他答案所提到的,這里與指標算術無關。在里面malloc你指定你想在記憶體中分配的位元組數。strlen(name)是字串char中name的數字,8 是字串文字“Hello”的位元組數,包括一個空終止符。
從這行評論開始,char *不取消參考 void 指標。它是 的變數宣告char*,其中右側可以隱式轉換為char*。
char * answer = malloc(strlen(name) 8); // syntax = (cast-type*) malloc(byte-size); answer has a * because malloc returns a void pointer. The * deferences the pointer
uj5u.com熱心網友回復:
假設函式必須回傳一個字串,例如
"Hello, N6DYN"
其中名稱“N6DYN”通過引數名稱傳遞給函式。
所以幻數 8 表示"Hello, "存盤在記憶體中的字串的大小,例如
{ 'H', 'e', 'l', 'l', 'o', ',', ' ', '\0' }
該函式可以通過以下方式宣告和定義,如下面的演示程式所示
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * helloWorld( const char * name )
{
const char *hello = "Hello, ";
char *answer = malloc( strlen( name ) strlen( hello ) 1 );
if ( answer != NULL )
{
strcpy( answer, hello );
strcat( answer, name );
}
return answer;
}
int main( void )
{
char name[100] = "";
printf( "Enter your name: " );
scanf( "
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/442966.html
