Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在其他函数中使用malloc更改字符串_C_String_Pointers_Malloc - Fatal编程技术网

在其他函数中使用malloc更改字符串

在其他函数中使用malloc更改字符串,c,string,pointers,malloc,C,String,Pointers,Malloc,使用malloc时,如何更改其他函数中的字符串 in main: char *myString; changeString(myString); changeString(char *myString){ myString = malloc((size) * sizeof(char)); myString[1] = 'a'; } 谢谢C中的参数是按值传递的。因此,要修改函数中的变量,必须将指针传递给它。例如,int*到int,以及char**到char* void

使用malloc时,如何更改其他函数中的字符串

in main: 

char *myString;
changeString(myString);


changeString(char *myString){

    myString = malloc((size) * sizeof(char));
    myString[1] = 'a';

}

谢谢

C中的参数是按值传递的。因此,要修改函数中的变量,必须将指针传递给它。例如,
int*
int
,以及
char**
char*

void changeString(char **myString){
    // free(*myString); // add when myString is allocated using malloc()
    *myString = malloc(2);
    (*myString)[0] = 'a';
    (*myString)[1] = '\0';
}

C中的参数是按值传递的。因此,要修改函数中的变量,必须将指针传递给它。例如,
int*
int
,以及
char**
char*

void changeString(char **myString){
    // free(*myString); // add when myString is allocated using malloc()
    *myString = malloc(2);
    (*myString)[0] = 'a';
    (*myString)[1] = '\0';
}

在main中分配内存,然后将分配内存的开始指针传递给函数。
还要将包含分配内存大小的变量传递给函数,以便确保新文本不会溢出分配的内存。
修改后的字符串可从main获得

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

void changeString(char *stringptr, int sz);

int main(void)
{
    const int a_sz = 16;
    /* allocate memory for string & test successful allocation*/
    char *myString = malloc(a_sz);
    if (myString == NULL) {
        printf("Out of memory!\n");
        return(1);
    }

    /* put some initial characters into String */
    strcpy(myString, "Nonsense");
    /* print the original */
    printf("Old text: %s\n", myString);

    /* call function that will change the string */
    changeString(myString, a_sz);

    /* print out the modified string */
    printf("New text: %s\n", myString);

    /* free the memory allocated */
    free(myString);
}

void changeString(char *stringptr, int sz)
{
    /* text to copy into array */
    char *tocopy = "Sense";
    /* only copy text if it fits in allocated memory
       including one byte for a null terminator */
    if (strlen(tocopy) + 1 <= sz) {
        strcpy(stringptr, tocopy);
    }
}
#包括
#包括
#包括
void changeString(char*stringptr,intsz);
内部主(空)
{
常数int a_sz=16;
/*为字符串分配内存&测试成功分配*/
char*myString=malloc(a_sz);
if(myString==NULL){
printf(“内存不足!\n”);
申报表(1);
}
/*将一些初始字符放入字符串中*/
strcpy(myString,“胡说八道”);
/*打印原稿*/
printf(“旧文本:%s\n”,myString);
/*调用将更改字符串的函数*/
changeString(myString,a_sz);
/*打印出修改后的字符串*/
printf(“新文本:%s\n”,myString);
/*释放分配的内存*/
免费(myString);
}
void changeString(char*stringptr,intsz)
{
/*要复制到数组中的文本*/
char*tocopy=“Sense”;
/*仅当文本适合分配的内存时才复制文本
包括空终止符的一个字节*/

如果(strlen(tocopy)+1在main中分配内存,则将分配内存的开始指针传递给函数。
还要将包含分配内存大小的变量传递给函数,以便确保新文本不会溢出分配的内存。
修改后的字符串可从main获得

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

void changeString(char *stringptr, int sz);

int main(void)
{
    const int a_sz = 16;
    /* allocate memory for string & test successful allocation*/
    char *myString = malloc(a_sz);
    if (myString == NULL) {
        printf("Out of memory!\n");
        return(1);
    }

    /* put some initial characters into String */
    strcpy(myString, "Nonsense");
    /* print the original */
    printf("Old text: %s\n", myString);

    /* call function that will change the string */
    changeString(myString, a_sz);

    /* print out the modified string */
    printf("New text: %s\n", myString);

    /* free the memory allocated */
    free(myString);
}

void changeString(char *stringptr, int sz)
{
    /* text to copy into array */
    char *tocopy = "Sense";
    /* only copy text if it fits in allocated memory
       including one byte for a null terminator */
    if (strlen(tocopy) + 1 <= sz) {
        strcpy(stringptr, tocopy);
    }
}
#包括
#包括
#包括
void changeString(char*stringptr,intsz);
内部主(空)
{
常数int a_sz=16;
/*为字符串分配内存&测试成功分配*/
char*myString=malloc(a_sz);
if(myString==NULL){
printf(“内存不足!\n”);
申报表(1);
}
/*将一些初始字符放入字符串中*/
strcpy(myString,“胡说八道”);
/*打印原稿*/
printf(“旧文本:%s\n”,myString);
/*调用将更改字符串的函数*/
changeString(myString,a_sz);
/*打印出修改后的字符串*/
printf(“新文本:%s\n”,myString);
/*释放分配的内存*/
免费(myString);
}
void changeString(char*stringptr,intsz)
{
/*要复制到数组中的文本*/
char*tocopy=“Sense”;
/*仅当文本适合分配的内存时才复制文本
包括空终止符的一个字节*/

if(strlen(复印件)+1但我应该如何更改函数中的指针,因为我尝试了它,但它不起作用。我不想返回它。我在internet上搜索了它,但它不起作用或没有直接描述。您的函数在一个全新的内存块上运行,该内存块分配并存储在局部变量
myString
中。这对调用方的字符串。此外,它会造成内存泄漏。对新分配内存的唯一引用是
myString
局部变量,该变量在函数终止时会消失。关于这一点有很多问题:为什么在C函数中修改变量对调用方的变量没有影响。但是我应该如何更改副作用,因为我尝试了它,但它不起作用。我不想返回它。我在互联网上搜索了这个,但它不起作用或没有直接描述。你的函数在一个全新的内存块上运行,该内存块被分配并存储在局部变量
myString
中。这对调用方的字符串没有影响。此外,它创建了一个内存泄漏。对新分配内存的唯一引用是
myString
局部变量,该变量在函数终止时消失。关于这一点,有很多问题:为什么修改C函数中的变量对调用方的变量没有影响。