我试图将字符串的某些部分复制到其他新字符串中,但是当我尝试复制并打印结果时,它会给我奇怪的输出。。我真的希望有人能帮忙。我有一种感觉,那就是缺少指针。。这是我的来源;

#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;
 }

编辑:
wierd输出是:
Bar code: ATL1203S14
Warehouse: ATL
ID: ♫203(♫>
Qualifier: S14u♫203(♫>

最佳答案

我想你的意思是'\0'而不是"\0"3而不是4

ware[4] = "\0";

尝试:
ware[3] = 0;

&中的getData(&barcode, &warehouse...)也没有用处。只需使用getData(barcode, warehouse...);

09-17 17:34