[英]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.