[英]What is the difference between char str[] and char *str as function parameters?
[英]Can't understand the difference between declaring a pointer char *str as str/&str?Whats the difference and what does it do?
老實說,這不是我的代碼。 和我一起學習的是我哥哥,但他比我領先。
請注意 function char *replaceWord()
中的char *str
和char *resultString
。
/*Suppose you have a template letter.txt. You have to fill in values to a template. Letter.txt looks something like this:
Thanks {{name}} for purchasing {{item}} from our outlet {{outlet}}. Please visit our outlet {{outlet}} for any kind of problems. We plan to serve you again soon.
You have to write a program that will automatically fill the template.For this, read this file and replace these values:
{{name}} - Harry
{{item}} - Table Fan
{{outlet}} - Ram Laxmi fan outlet
Use file functions in c to accomplish the same.*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * replaceWord(const char * str, const char * oldWord, const char * newWord)
{
char * resultString;
int i, count = 0;
int newWordLength = strlen(newWord);
int oldWordLength = strlen(oldWord);
for (i = 0; str[i] != '\0'; i++)
{
if (strstr(&str[i], oldWord) == &str[i])
{
count++;
//Jumping over the word and continuing
i = i + oldWordLength - 1;
}
}
//dynamically allocation memory to resultString since it can be big or samll depending on the size of the newWord.
/*i = old string length , count = no. of times the word appeared in the string,
newWordLength-oldWordLength=difference between the new word and the old word
+1 for the null character '\0'
Basically we are saying that add the size required for the newWord to the strings length i.e i;
*/
resultString = (char *)malloc(i + count * (newWordLength - oldWordLength) + 1);
i = 0; //refreshing the i for the while loop
while (*str)
{
if (strstr(str, oldWord) == str)
{
strcpy(&resultString[i], newWord);
i += newWordLength;
str += oldWordLength;
}
else
{
resultString[i] = *str;
i+=1;
str+=1;
}
}
resultString[i] = '\0';
return resultString;
}
int main()
{
FILE *ptr = NULL;
FILE *ptr2 = NULL;
ptr = fopen("letter.txt", "r"); //where the template is stored
ptr2 = fopen("newLetter.txt", "w"); //where the new bill will be stored.
char str[200];
fgets(str, 200, ptr); //store the bill template in the str variable.
printf("The original bill template is : %s\n", str);
//Calling the replacing fucntion
char *newStr = str; //newStr will store the new bill i.e generated
newStr = replaceWord(str, "{{name}}", "Mary");
newStr = replaceWord(newStr, "{{item}}", "Waffle Machine");
newStr = replaceWord(newStr, "{{outlet}}", "Belgium Waffle");
printf("\nThe bill generated is:\n%s", newStr);
fprintf(ptr2, "%s", newStr);
fclose(ptr);
fclose(ptr2);
return 0;
}
有人可以解釋為什么指針*str
和*resultString
在程序中以不同的方式表示,它們在做什么? 有時它是*str
、 &str
或str[i]
。 請解釋。 我知道指針用於保存其他變量的地址,但這段代碼對我來說仍然是個謎。 還有為什么 function 是指針?
注意:當我問怎么做時,“他說這就是它的工作原理”。 請幫忙。; 我無法專注於其他事情。 如果你不能解釋;解釋鏈接也可以。
Sometimes it's *str, &str or str[i]
那些是運營商。
str
是char
的一個指針,並且在它上面有一個*
會取消引用它。 這意味着它從它指向的 memory 中獲取值。 指針可能並不總是指向變量,它可以是任意 memory 地址。 但是取消引用不屬於您的 memory 將導致分段錯誤,這是我最喜歡的錯誤,幾乎每次在處理 arrays 時都會發生。
這與*(str + i)
相同。 這意味着它將 memory 地址增加i * sizeof(<datatype of what str points to>)
。 然后它從那個增加的地址中獲取值。 這用於獲取數組的元素。
這只是給出了變量str
的地址,它是一個指針。 因此,它返回一個指向指針的指針(即str
)。 可以存在指向指針的指針。
function 不是指針。 相反,它返回一個指針*resultString
。 這樣就可以返回一個字符串。 該字符串已在此行中初始化:
resultString = (char *)malloc(i + count * (newWordLength - oldWordLength) + 1);
解釋這一點的評論並不完整。
//dynamically allocation memory to resultString since it can be big or samll depending on the size of the newWord.
/*i = old string length , count = no. of times the word appeared in the string,
newWordLength-oldWordLength=difference between the new word and the old word
+1 for the null character '\0'
Basically we are saying that add the size required for the newWord to the strings length i.e i;
*/
它還遺漏了使用malloc
而不是正常分配的一個關鍵原因。 malloc
在所有函數和線程之間共享的堆上分配變量。 雖然正常初始化會將其分配在堆棧上,當 function 結束時彈出堆棧。 所以,function 跟棧后沒用,所以應該在堆上使用。 它也適用於動態分配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.