微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用strcpy在C中为指针正确分配值

如何解决使用strcpy在C中为指针正确分配值

我只需要从char数组中获取奇数,然后使用指针将它们复制到正确大小的动态内存中即可。

但是,当运行我的程序时,它可以与某些输入字符串正常工作,而不能与其他输入字符串正常工作。我做错什么了吗?我似乎不知道发生了什么。

/* A.) Include the necessary headers in our program */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRING_LENGTH 32

int main() {
    /* B.) Declare char array with inital size of 32 */
    char input_string[MAX_STRING_LENGTH];

    /* C.) Recieve user input.
           Can save the first 31 characters in the array with 32nd reserved for '\0' */
    printf("Enter a string of characters: ");

    /* D.) Using the technique we discussed to limit the string to 31 charaters */
    scanf("%31s",input_string);
    printf("\n");

    /* Will be used to determine the exact amount of dynamic memory that will be allocated later */
    int odd_value_count = 0;
    printf("Odd Characters: ");
    for(int i = 0; i < strlen(input_string); i++) {
        if(i % 2 != 0) {
            printf("%c ",input_string[i]);
            odd_value_count++;
        }
    }

    printf("\n");
    printf("Odd value count: %d\n",odd_value_count);

    /* E.) Delecaring the pointer that will hold some part of the input_string
           Pointer will be a char type */
    char *string_pointer;

    /* G.) Allocating the space before the copy using our odd value count */
    /* H.) The exact amount of space needed is the sizeof(char) * the odd value count + 1 */
    string_pointer = (char *)malloc(sizeof(char) * (odd_value_count + 1));

    if (string_pointer == NULL) {
        printf("Error! Did not allocte memory on heap.");
        exit(0);
    }


    /* F.) copying all charcters that are on the odd index of the input_string[] array
           to the memory space pointed by the pointer we delcared */
    printf("copIED: ");
    for (int i = 0; i < strlen(input_string); ++i) {

        if(i % 2 != 0) {
            strcpy(string_pointer++,&input_string[i]);
            printf("%c ",input_string[i]);
        }
    }

    /* Printing out the string uses the pointer,however we must subtract odd_value_count to
       position the pointer back at the original start address */
    printf("\n%s\n",string_pointer - odd_value_count);

    return 0;

}

此输入字符串:01030507 可以正常工作并复印和打印:1357

输入字符串:testing 复制etn,但打印etng

我无法弄清楚为什么对于某些字符串,即使我从未复制过该值,它也会在末尾打印出多余的字符。

解决方法

在将字符串指针中的奇数字符复制完毕后,您需要将字符串终止为空{strong} ,例如*string_pointer = '\0';-在该循环之后,将字符串终止为null。

How to add null terminator to char pointer,when using strcpy中了解更多吗?

,

在例程的末尾,您将需要使null终止该字符串,否则您将没有一个字符串,而只是一个char数组,则可以使用<div> <ul> <li> <input type="radio" value="0" name="radio_choice" id="radio_choice_0"> <label>No Option</label> </li> <li> <input type="radio" value="10" name="radio_choice" id="radio_choice_10"> <label>Option 1 ($10)</label> </li> <li> <input type="radio" value="30" name="radio_choice" id="radio_choice_30"> <label>Option 2 ($30)</label> </li> </ul> </div> ,它已经指向末尾要保存的字符串中的一个:

string_pointer

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。