memcpy的使用出错

src是好的,但是memcpy的dst就是空的,不知道哪里出了问题,求指点。

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

#define len 20

char *BDS_TIME;
char *BDS_DATE;
char* p[len];
int i;
char uart3[60]={'$','G','N','R','M','C',',','0','2','2','4','5','6',',','V',',',',',',',',',',',',',',','1','7','0','8','2','1',',',',',',','M'};

void finddata(char* pending);

int main()
{
    finddata(uart3);

    return 0;
}

void finddata(char* pending)
{
    p[0]=strtok(pending,",");
    if(p!=NULL)
    {
        for(i=1;i<len;i++)
        {
            p[i]=strtok(NULL,",");
            if(p==NULL)
            {
                break;
            }
        }
    }
    // printf("%s,%s",p[1],p[3]);

    memcpy(BDS_TIME,p[1],6);
    memcpy(BDS_DATE,p[3],6);
    printf("%s,%s\r\n",BDS_TIME,BDS_DATE);
}

BDS_TIME和BDS_DATE你还没有分配空间,当然不能拷贝了。另外数组要赋值为空,否则%s格式化输出有问题

#include <stdio.h>
#include <string.h>
#define len 20
char *BDS_TIME;
char *BDS_DATE;
char* p[len];
int i;
char uart3[60]={'$','G','N','R','M','C',',','0','2','2','4','5','6',',','V',',',',',',',',',',',',',',','1','7','0','8','2','1',',',',',',','M'};
void finddata(char* pending);
int main()
{
    finddata(uart3);
    return 0;
}
void finddata(char* pending)
{
    p[0]=strtok(pending,",");
    if(p!=NULL)
    {
        for(i=1;i<len;i++)
        {
            p[i]=strtok(NULL,",");
            if(p==NULL)
            {
                break;
            }
        }
    }
    BDS_TIME = (char*)malloc(7);
    memset(BDS,TIME,7,0);
    BDS_DATE = (char*)malloc(7);
    memset(BDS,DATA,7,0);
    memcpy(BDS_TIME,p[1],6);
    memcpy(BDS_DATE,p[3],6);
    printf("%s,%s\r\n",BDS_TIME,BDS_DATE);
}

这两个变量定义成字符数组就好了,之前只是字符指针没有空间存储,所以错了。

#include <stdio.h>
#include <string.h>
#define len 20
char BDS_TIME[20];
char BDS_DATE[20];
char* p[len];
int i;
char uart3[60]={'$','G','N','R','M','C',',','0','2','2','4','5','6',',','V',',',',',',',',',',',',',',','1','7','0','8','2','1',',',',',',','M'};
void finddata(char* pending);
int main()
{
    finddata(uart3);
    return 0;
}
void finddata(char* pending)
{
    p[0]=strtok(pending,",");
    if(p!=NULL)
    {
        for(i=1;i<len;i++)
        {
            p[i]=strtok(NULL,",");
            if(p==NULL)
            {
                break;
            }
        }
    }
     //printf("%s,%s",p[1],p[3]);
    memcpy(BDS_TIME,p[1],6);
    memcpy(BDS_DATE,p[3],6);
    printf("%s,%s\r\n",BDS_TIME,BDS_DATE);
}

img

char *BDS_TIME;
char *BDS_DATE;
你这本来就是空的,这个指针还要分配buffer的
BDS_TIME = malloc(xxx);
BDS_DATE= malloc(xxx);