简体   繁体   中英

C program giving weird output

I am trying to copy certain parts of a string into other, new strings, but when i try to do it and print the results it gives me weird output.. I really hope someone can help. I have a feeling that it is something about missing pointers.. Here is my source;

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

void getData(char code[], char ware[], char prod[], char qual[])
{
    printf("Bar code: %s\n", code);

    /* Copy warehouse name from barcode */
    strncpy(ware, &code[0], 3);
    ware[4] = "\0";

    strncpy(prod, &code[3], 4);
    prod[5] = "\0";

    strncpy(qual, &code[7], 3);
    qual[4] = "\0";
}

int main(){

    /* allocate and initialize strings */
    char barcode[] = "ATL1203S14";
    char warehouse[4];
    char product[5];
    char qualifier[4];

    getData(&barcode, &warehouse, &product, &qualifier);

    /* print it */
    printf("Warehouse: %s\nID: %s\nQualifier: %s", warehouse, product, qualifier);

    return 0;
 }

EDIT:

The wierd output is:

Bar code: ATL1203S14
Warehouse: ATL
ID: ♫203(♫>
Qualifier: S14u♫203(♫>

I think you meant '\\0' instead of "\\0" and 3 instead of 4 :

ware[4] = "\0";

Try:

ware[3] = 0;

Also the & in getData(&barcode, &warehouse...) are useless. Just use getData(barcode, warehouse...); .

You're writing past the end of the chars in your getData() function. You've defined char product[5] , which allocates 5 bytes of memory. That gives you array indexes 0,1,2,3,4. In getData, you write the product's null terminator to index 5, which is past the end of product, and will overwrite the next var's first character.

The same applies for barecode, warehouse, and qualifier.

Arrays in C and C++ are zero-based. The last index is one less than the length. You're setting a value in the memory after the array, for each of the arrays ware, prod and qual .

For example, instead of

 char warehouse[4];
 ware[4] = "\0";

you'd want:

 char warehouse[4];
 ware[3] = "\0";
getData(&barcode, &warehouse, &product, &qualifier);

This is not the way you should call getData . getData takes pointers, arrays are automatically converted to pointers, so theres no need to use the address-of operator & .

You should use

getData(barcode, warehouse, product, qualifier);

The sizes of the strings inside main() don't include a place for the sentinel.

You need to have:

char warehouse[5];
char product[6];
char qualifier[5];

Also, You are assigning a pointer to the string "\\0" into a character, where you should be assigning the character '\\0' itself.

I think I'd do things a bit differently. In particular, strncpy is almost never really useful (I'm reasonably certain it was invented for file names in the original Unix FS, and while it fits their specific requirements quite nicely, those requirements are sufficiently unusual that it's rarely good for much of anything else).

Instead, I'd use sscanf: sscanf(code, "%4c%5c%4c", ware, prod, qual);

Your question does not make it clear whether this is really correct. As others have pointed out, you're writing past the ends of the space you've allocated. Above, I've assumed you specified the number of characters you want to copy, so you'd have to expand each of the allocations by one character to make room for the terminator. Alternative, if you've already left room for the terminator and want one fewer character copied, you'd have to reduce each of the lengths above by one so the format string would be "%3c%4c%3c".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM