Assigning value to pointer correctly in C using strcpy()

Viewed 177

I need to take only the odd values from a char array and copy them into correctly sized dynamic memory using a pointer.

However when running my program it works correctly with certain input strings and not with others. Is there something that I'm doing wrong? I can't seem to figure out what's going on.

/* 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;

}

This input string: 01030507 works fine and copies & prints: 1357

The input string: testing Copies etn but prints etng.

I cant figure out why for some strings it prints out the extra character at the end when I never even copy the value over.

2 Answers

In the end of your routine you will need to null terminate the string, otherwise you don't have a string you just have a char array, you can use string_pointer which is already pointing to one past the end of the string you want to save:

//...
for (int i = 0; i < strlen(input_string); ++i) {

    if(i % 2 != 0) {
        strcpy(string_pointer++, &input_string[i]);
        //as you are copying characters, you can do this:
        //*string_pointer++ = input_string[i]; 
        //instead of strcpy
        printf("%c ", input_string[i]);
    }
}
*string_pointer = '\0'; // <-- here
//...
Related