2012-02-28 94 views
1

我有这些变量在我的调试器设置:无法复制串

//调试值不代码

RELATIVE_PATH = “./bin/Debug”

的strlen(RELATIVE_PATH)= 11

RELATIVE_PATH [11] = 0 '\ 000' //如图Eclipse调试器

在我的代码然后执行:

//**My Code** 
struct dirs_later { 
    const char *findme; 
    struct dirent *dptr; 
    struct stat this_lstat; 
    char *relative_path; 
    const char *type_str; 
    DIR * dir; 
}; 

.... 
struct dirs_later *new_dir = malloc(sizeof(struct dirs_later*)); 
... 
char *relative_path2 = strdup(relative_path); 
if (NULL != new_dir) { 
    new_dir->findme = findme; 
    new_dir->dptr = dptr; 
    new_dir->this_lstat = *this_lstat; 
    new_dir->relative_path = relative_path2; 
    new_dir->type_str = type_str; 
    } 

但是然后调试器显示在new_dir-> relative_path = relative_path2之后。然后在调试器:

//调试值不代码

relative_path2 = “& \ 275 \ 001” 和

的strlen(relative_path2)= 3

我也是在我的代码尝试这个而不是:

//**My Code** 
char *relative_path2 = malloc(strlen(relative_path) + 1 * sizeof(char)); 
//check for NULL 
strcpy(relative_path2, relative_path); 

,我也得到相同的结果

+0

不知道你想用'relative_path [11] = \ 0'来做什么......字符串已经被终止了,我相信这会影响现有的空终止符后的字节*。 – 2012-02-28 00:44:21

+0

'sizeof(char)'是1.为什么写它? – 2012-02-28 00:44:39

+0

如果'strlen(str)== 10',那么'str [10]'应该是NULL,而不是'str [11]'。 – 2012-02-28 00:44:48

回答

2

应行

struct dirs_later *new_dir = malloc(sizeof(struct dirs_later*)); 

struct dirs_later *new_dir = malloc(sizeof(struct dirs_later)); 

只要你想创建一个结构上堆的空间,不只是一个指针。

+0

就是这样。谢谢 – user994165 2012-02-28 01:27:32