char* 参数值在函数内部改变
Values of char* parameter changing inside function
我想创建一个程序来创建一个二维数组并从 stdin 填充它以供以后操作。最初,我将其设计为函数 takeInput() 接受用户的输入和 returns 一个包含所有一位整数的单个字符串的 char*。然后,我的目的是获取此 char* 并将其作为参数传递给第二个函数 createArray() ,该函数将相应地填充数组。
当我的参数 char* 的值在第二个函数中开始改变(丢失值)时,就会出现问题。
注意: 我已经在第一个函数中使用 malloc 为 char* 分配了内存,目的是不让它的值成为函数的局部值。
我的主图是这样的:
#include <stdio.h>
#include "functions.h"
int main(int argc, const char * argv[]) {
const char *string = takeInput();
int **initState = createArray(3, 3, string);
return 0;
}
这是第一个函数:
const char* takeInput() {
char string2[10];
/* some code */
char *string0 = malloc(sizeof(string2));
string0 = string2;
return string0;
}
这是第二个函数:
int **createArray(int rows, int columns, const char *string) {
int **array;
int traverse = 0;
// allocating memory for number of rows
array = malloc(rows * sizeof(int*));
// allocating memory for number of columns
for(int i = 0 ; i < rows ; i++) {
array[i] = malloc(columns * sizeof(int));
}
// populating indices in 2D array with values from string
for(int i = 0 ; i < rows ; i++) {
for(int j = 0 ; j < columns ; j++) {
// converting chars to digits or the '_' char
if (string[traverse] != 95) {
array[i][j] = (string[traverse] - 48);
}
else {
array[i][j] = 48;
}
traverse++;
}
}
return array;
}
const char *string
的值在我的代码到达第一个 for 循环后立即更改,并在其余代码中减小大小。非常感谢任何ideas/suggestions!
程序有未定义的行为,因为在这个函数中
const char* takeInput() {
char string2[10];
/* some code */
char *string0 = malloc(sizeof(string2));
string0 = string2;
return string0;
}
指向本地数组的指针
string0 = string2;
return string0;
返回。此外,该函数存在内存泄漏,因为分配了内存并将其地址分配给指针 string0,然后重新分配指针。
如果数组 string2 包含一个字符串,那么您可以使用标准 C 函数 strcpy
like
strcpy( string0, string2 );
否则使用函数memcpy
.
请注意,在此代码段中使用 95 或 48 等幻数是个坏主意
// converting chars to digits or the '_' char
if (string[traverse] != 95) {
array[i][j] = (string[traverse] - 48);
}
else {
array[i][j] = 48;
}
看来你的意思如下
// converting chars to digits or the '_' char
if (string[traverse] != '_' ) {
array[i][j] = (string[traverse] - '0');
}
else {
array[i][j] = '0';
}
虽然可能在 else 语句中你的意思是 0 而不是 '0'
else {
array[i][j] = 0;
}
你return一个局部变量的地址,它的生命周期会在函数returns结束。之后访问此地址会产生未定义的行为。
const char* takeInput() {
char string2[10];
char *string0 = malloc(sizeof(string2));
string0 = string2; // NOTE: string2 is not copied; string0 will just point to the address of string2 then
return string0; // here you actually return the address of string2
}
改用strcpy
:
const char* takeInput() {
char string2[10] = { 0 };
// some code...
char *string0 = malloc(sizeof(string2));
strcpy(string0,string2);
return string0;
}
我想创建一个程序来创建一个二维数组并从 stdin 填充它以供以后操作。最初,我将其设计为函数 takeInput() 接受用户的输入和 returns 一个包含所有一位整数的单个字符串的 char*。然后,我的目的是获取此 char* 并将其作为参数传递给第二个函数 createArray() ,该函数将相应地填充数组。
当我的参数 char* 的值在第二个函数中开始改变(丢失值)时,就会出现问题。
注意: 我已经在第一个函数中使用 malloc 为 char* 分配了内存,目的是不让它的值成为函数的局部值。
我的主图是这样的:
#include <stdio.h>
#include "functions.h"
int main(int argc, const char * argv[]) {
const char *string = takeInput();
int **initState = createArray(3, 3, string);
return 0;
}
这是第一个函数:
const char* takeInput() {
char string2[10];
/* some code */
char *string0 = malloc(sizeof(string2));
string0 = string2;
return string0;
}
这是第二个函数:
int **createArray(int rows, int columns, const char *string) {
int **array;
int traverse = 0;
// allocating memory for number of rows
array = malloc(rows * sizeof(int*));
// allocating memory for number of columns
for(int i = 0 ; i < rows ; i++) {
array[i] = malloc(columns * sizeof(int));
}
// populating indices in 2D array with values from string
for(int i = 0 ; i < rows ; i++) {
for(int j = 0 ; j < columns ; j++) {
// converting chars to digits or the '_' char
if (string[traverse] != 95) {
array[i][j] = (string[traverse] - 48);
}
else {
array[i][j] = 48;
}
traverse++;
}
}
return array;
}
const char *string
的值在我的代码到达第一个 for 循环后立即更改,并在其余代码中减小大小。非常感谢任何ideas/suggestions!
程序有未定义的行为,因为在这个函数中
const char* takeInput() {
char string2[10];
/* some code */
char *string0 = malloc(sizeof(string2));
string0 = string2;
return string0;
}
指向本地数组的指针
string0 = string2;
return string0;
返回。此外,该函数存在内存泄漏,因为分配了内存并将其地址分配给指针 string0,然后重新分配指针。
如果数组 string2 包含一个字符串,那么您可以使用标准 C 函数 strcpy
like
strcpy( string0, string2 );
否则使用函数memcpy
.
请注意,在此代码段中使用 95 或 48 等幻数是个坏主意
// converting chars to digits or the '_' char
if (string[traverse] != 95) {
array[i][j] = (string[traverse] - 48);
}
else {
array[i][j] = 48;
}
看来你的意思如下
// converting chars to digits or the '_' char
if (string[traverse] != '_' ) {
array[i][j] = (string[traverse] - '0');
}
else {
array[i][j] = '0';
}
虽然可能在 else 语句中你的意思是 0 而不是 '0'
else {
array[i][j] = 0;
}
你return一个局部变量的地址,它的生命周期会在函数returns结束。之后访问此地址会产生未定义的行为。
const char* takeInput() {
char string2[10];
char *string0 = malloc(sizeof(string2));
string0 = string2; // NOTE: string2 is not copied; string0 will just point to the address of string2 then
return string0; // here you actually return the address of string2
}
改用strcpy
:
const char* takeInput() {
char string2[10] = { 0 };
// some code...
char *string0 = malloc(sizeof(string2));
strcpy(string0,string2);
return string0;
}