小编Lee*_*eet的帖子

如果我已经为字符串b指定了值,则无法将字符串a复制到字符串b

当我取消注释第13行时,程序到达第25行然后退出.在复制到name_2的值之前,我给name_1赋值的可能是什么?先感谢您.

#include <stdio.h>
#include <stdlib.h>

void strcpy(char* name_1, char* name_2);

int main()
{
    char *name_1, *name_2;
    name_1 = (char*)malloc(20*sizeof(char));
    name_2 = (char*)malloc(20*sizeof(char));
    printf("Insert name asap!: ");
    gets(name_2);
    //name_1 = "boufos";
    printf("The first name is: %s and the second: %s\n", name_1, name_2);
    strcpy(name_1, name_2);
    printf("The first name is: %s and the second: %s\n", name_1, name_2);
    return 0;
}

void strcpy(char* name_1, char* name_2)
{
    int i = 0;
    while (name_2[i] != '\0')
    {
        name_1[i] = name_2[i];
        i++;
    }
    name_1[str_length(name_2)] = '\0'; …
Run Code Online (Sandbox Code Playgroud)

c pointers

2
推荐指数
2
解决办法
71
查看次数

链表作为函数的参数

程序不会按预期打印列表的值.它打印的东西必须是一个内存地址imo.我一直试图找到独奏解决方案,但到目前为止无济于事.我将不胜感激.

#include <stdio.h>

typedef struct node
{
    int val;
    struct node * next;
} node_t;

void print_list(node_t * head);

void main()
{
    node_t * head = NULL;
    head = malloc(sizeof(node_t));
    if (head == NULL)
        return 1;
    head->val = 1;
    head->next = malloc(sizeof(node_t));
    head->next->val = 2;
    head->next->next = malloc(sizeof(node_t));
    head->next->next->val = 3;
    head->next->next->next = malloc(sizeof(node_t));
    head->next->next->next->val = 18;
    head->next->next->next->next = NULL;

    print_list(&head);
    system("pause");
}

void print_list(node_t * head) {
    node_t * current = head;

    while (current != NULL) {
        printf("%d\n", current->val); …
Run Code Online (Sandbox Code Playgroud)

c pointers linked-list

0
推荐指数
1
解决办法
597
查看次数

标签 统计

c ×2

pointers ×2

linked-list ×1