c语言字符串拷贝问题。。

软件和网站开发以及相关技术探讨
回复
drongh
帖子: 1038
注册时间: 2007-01-10 9:32

c语言字符串拷贝问题。。

#1

帖子 drongh » 2014-12-14 16:15

下面的程序是自己写一个strncpy函数。从source里拷贝10个字符到target里。

代码: 全选

#include "stdio.h"
#include "string.h"
#define SIZE 10
char * my_strncpy(char * target, char * source, int n);

int main(int argc, char const *argv[])
{
    char * target;
    char source[] = "hello, world";
    target = my_strncpy(target, source, SIZE);
    puts(target);
    return 0;
}

char * my_strncpy(char * target, char * source, int n)
{   
    int index;
    for (index = 0; index < n; index++)
    {   
        if (index < strlen(source)) 
            target[index] = source[index];
        else
            break;
    }
    target[index] = '\0';
    return target;
}
编译没问题,结果运行出现错误。信息如下:

代码: 全选

段错误 (核心已转储)
这是啥问题,没见过呢,是不是指针用的有问题?
大家给指点一下。谢谢。
cao627
帖子: 992
注册时间: 2007-12-05 10:57
系统: ubuntu14.04
来自: 金山

Re: c语言字符串拷贝问题。。

#2

帖子 cao627 » 2014-12-14 17:50

char * target;

只是定义了一个指向存放一个字符的空间的指针,这条语句只能保证这个指针指向的一个字节空间是你允许访问的。
但你却对这一个字节空间后的空间做了操作。
drongh
帖子: 1038
注册时间: 2007-01-10 9:32

Re: c语言字符串拷贝问题。。

#3

帖子 drongh » 2014-12-14 18:02

代码: 全选

#include "stdio.h"
#include "string.h"
#define SIZE 10

char * my_strncpy(char * target, char * source, int n);

int main(int argc, char const *argv[])
{
    char target[100];
    char * ptr;
    char source[] = "hello, world";
    ptr = my_strncpy(target, source, SIZE);
    puts(ptr);
    return 0;
}

char * my_strncpy(char * target, char * source, int n)
{   
    int index;
    for (index = 0; index < n; index++)
    {   
        if (index < strlen(source)) 
            target[index] = source[index];
        else
            break;
    }
    target[index] = '\0';
    return target;

}
我这样该了一下,就好了。
虽然好了,但还是不知道错在哪里,为什么错了。
rosynirvana
帖子: 893
注册时间: 2011-02-14 17:46

Re: c语言字符串拷贝问题。。

#4

帖子 rosynirvana » 2014-12-15 13:12

cao627 写了:char * target;

只是定义了一个指向存放一个字符的空间的指针,这条语句只能保证这个指针指向的一个字节空间是你允许访问的。
但你却对这一个字节空间后的空间做了操作。
他只定义了指针变量,没有分配空间,没法保证有多少空间能用
rosynirvana
帖子: 893
注册时间: 2011-02-14 17:46

Re: c语言字符串拷贝问题。。

#5

帖子 rosynirvana » 2014-12-15 13:16

drongh 写了:

代码: 全选

#include "stdio.h"
#include "string.h"
#define SIZE 10

char * my_strncpy(char * target, char * source, int n);

int main(int argc, char const *argv[])
{
    char target[100];
    char * ptr;
    char source[] = "hello, world";
    ptr = my_strncpy(target, source, SIZE);
    puts(ptr);
    return 0;
}

char * my_strncpy(char * target, char * source, int n)
{   
    int index;
    for (index = 0; index < n; index++)
    {   
        if (index < strlen(source)) 
            target[index] = source[index];
        else
            break;
    }
    target[index] = '\0';
    return target;

}
我这样该了一下,就好了。
虽然好了,但还是不知道错在哪里,为什么错了。
因为之前那个ptr没分配空间,这个target在main的栈上分配了100字节

另外如果是要仿写标准库中的strncpy,注意它的行为是这样的
Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.

No null-character is implicitly appended at the end of destination if source is longer than num. Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow).

destination and source shall not overlap (see memmove for a safer alternative when overlapping).
cao627
帖子: 992
注册时间: 2007-12-05 10:57
系统: ubuntu14.04
来自: 金山

Re: c语言字符串拷贝问题。。

#6

帖子 cao627 » 2014-12-15 15:56

rosynirvana 写了:
cao627 写了:char * target;

只是定义了一个指向存放一个字符的空间的指针,这条语句只能保证这个指针指向的一个字节空间是你允许访问的。
但你却对这一个字节空间后的空间做了操作。
他只定义了指针变量,没有分配空间,没法保证有多少空间能用
对的,我错了。
声明一个指针,只是开辟一个了一块其内容作为地址的空间,但是并没有在这块空间中写进一个允许用户访问的空间的合法地址。
回复