Chinaunix首页 | 论坛 | 博客
  • 博客访问: 701885
  • 博文数量: 126
  • 博客积分: 2944
  • 博客等级: 上校
  • 技术积分: 1160
  • 用 户 组: 普通用户
  • 注册时间: 2005-02-17 11:09
个人简介

文章分类

全部博文(126)

文章存档

2022年(1)

2018年(1)

2017年(5)

2016年(5)

2013年(5)

2012年(21)

2011年(24)

2010年(1)

2009年(2)

2008年(12)

2007年(6)

2006年(19)

2005年(24)

分类: C/C++

2007-11-02 14:18:00

 

#include

#include


typedef struct nd {
    struct nd* nextNode;
    char* s1;
    char* s2;
    char i;
} errNode;

int main()
{
    errNode* head;
    errNode* currentNode;
    
    char* s3 = "abc";
    char* s4 = "123";
    
    head = (errNode*)malloc(sizeof(errNode) + strlen(s3) + strlen(s4) + 2);
    
    if(NULL == head)
        return 1;
    
    head->s1 = (char*)head + sizeof(errNode);
    head->s2 = head->s1 + strlen(s3) + 1;
    
    strcpy(head->s1, s3);
    strcpy(head->s2, s4);
   

    printf("%s\n", head->s1);
    printf("%s\n", head->s2);


    free(head);

   return 0;
}


相关点:
    1. char* strcpy(char* dst, char* src) 的作用是把一个字符串拷贝到另一个字符串中, 这两个字符串在内存中不能重叠;
    2. void* memcpy(void* dest, void* src, unsigned int count) 的作用是把内存中 count 个数据拷贝到别处, 内存位置也不能重叠; 数据包括'\0'字符;
    3. void* memmove(void* dest, void* src, unsigned int count) 与memcpy的不同在于内存位置可以重叠; 也即 src 的内容可能会被改变;
    4. 结构体中不同类型的数据占用空间也许相同, 并且不同的排放顺序可能改变结构体的占用空间;
阅读(7145) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~