-1

i want to type a sequence of chars and save them with a temporary array. After that, i want to create the actual array with a certain size with the values of the temporary array. Here is the code:

#include <stdio.h>

int main()
{
    char c;
    char temp[100];
    char array[24];
    int i;
    char *ptrtemp = temp;


    // create a temporary array with getchar function
    while(1) {

        c = getchar();

        if(c == '\n')
            break;

        *ptrtemp = c;
        i++;
        ptrtemp++;

    }

    // type wrong in case of wrong size
    if(i != 24) {

        printf("Data is wrong");
        exit(0);
    }

    char *ptrarray = array;
    char *ptrtemp2 = temp;

    // create the actual array
    for(i=0;i<24;i++) {

        *ptrarray = *ptrtemp2;
        if(i == 23)
            break;
        ptrarray++;
        ptrtemp2++;
    }

    //printing the actual array
    printf("\n%s\n", array);
}

However, I get interesting elements after the actual sequence. The array's size was stated as 24 but 25th, 26th, 27th etc. elements are being also printed.

Here is what I get

Every time I try, I see different extra chars. Can anyone explain what is happening here?

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Undefined behavior for using the value of an object with automatic storage duration while it is indeterminate. – EOF Apr 23 '17 at 21:40
  • `int i;` --> `int i = 0;` – BLUEPIXY Apr 23 '17 at 21:40
  • 1
    Possible duplicate of [printf outputting garbage instead of specific characters](http://stackoverflow.com/questions/13567969/printf-outputting-garbage-instead-of-specific-characters) – Raymond Chen Apr 23 '17 at 21:42
  • create array with 25 elements and null terminate like this array[24] = 0; – samgak Apr 23 '17 at 21:42
  • its probably throwing out garbage data from memory because you never initialized the unused values set them to something and this should fix the issue – Chris Hutchison Apr 23 '17 at 21:43
  • adding '\0' char to the end helped. Thanks. –  Apr 23 '17 at 21:54
  • I have a prepped answer for this, but it would bring much to your question if you stated what the *problem* was you were trying to solve, not the issues you're having with *the way you're trying to solve it.* I suspect you're trying to consume an entire line of text (terminated by a newline), then store *exactly* the first twenty four characters into your `array` as a terminated string, discarding the remaining data as unused, all while using only `getchar()` to do it. But it isn't at-all-clear that is the actual problem. There are *plenty* of *issues* in posted code regardless. – WhozCraig Apr 23 '17 at 22:59

3 Answers3

1

You are doing the stuff far too complicated.

First, as already denoted, i is not initialised. Second, you do not leave space for the terminating 0 in the array. Finally, you can easily write to your array directly:

char array[24 + 1]; // need space for the terminating 0 character

for(int i = 0; i < 24; ++i)
{
    // write to array directly:
    array[i] = getchar();
    if(array[i] == '\n')
    {
        printf("Data is wrong");
        return 0;
    }
}

array[24] = 0; // this is important if using %s!
printf("\n%s\n", array);

Actually, you have an alternative, as you know you always want to print exactly 24 characters:

char array[24]; // skipping(!) space for the terminating 0 character

for(int i = 0; i < 24; ++i)
{
    // just as above
}

// NOT now (would be UB, as array is too short):
// array[24] = 0;

// but now need to give precision to tell how many bytes to write
// (maximally; if a terminating 0 would be encountered before,
// printf would stop there)
printf("\n%.24s\n", array);
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
0

I modified the following:

  1. Initialized i to 0 in the beginning.
  2. memset(temp, 0, sizeof(temp));
  3. Added following to make sure that it does not cross array boundary.

if (i == 99)
{
    break;
}
  1. Similarly added following for array variable.

memset(array, 0, sizeof(array));
  1. Following check is done beginning of for loop to make sure it does not write to 24th element of array.

if(i == 23)
     break;
#include <stdio.h>

int main()
{
    char c;
    char temp[100];
    char array[24];
    int i = 0; //Should be initialized to 0
    char *ptrtemp = temp;

    memset(temp, 0, sizeof(temp));

    // create a temporary array with getchar function
    while(1) {

        c = getchar();

        if(c == '\n')
            break;

        *ptrtemp = c;
        i++;
        ptrtemp++;

        if (i == 99)
        {
           break;
        }

    }

    char *ptrarray = array;
    char *ptrtemp2 = temp;

    memset(array, 0, sizeof(array));

    // create the actual array
    for(i=0;i<24;i++) {
        if(i == 23)
            break;
        *ptrarray = *ptrtemp2;
        ptrarray++;
        ptrtemp2++;
    }

    //printing the actual array
    printf("\n%s\n", array);
}

Let me know if you face any problem.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Pabitra Dash
  • 1,461
  • 2
  • 21
  • 28
-1

Two problems:

  1. You are using i uninitialized in your code. (I don't think the issue you are seeing is because of that)

  2. All C-strings need to be '\0' terminated. In order to ensure that it is the case, you can always do a:

    memset(array, 0, sizeof(array));

Furquan
  • 676
  • 3
  • 5