【发布时间】:2011-11-03 17:09:28
【问题描述】:
我正在尝试将字符串的某些部分复制到其他新字符串中,但是当我尝试这样做并打印结果时,它给了我奇怪的输出。我真的希望有人能提供帮助。我有一种感觉,这与缺少指针有关。这是我的来源;
#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;
}
编辑:
奇怪的输出是:
Bar code: ATL1203S14
Warehouse: ATL
ID: ♫203(♫>
Qualifier: S14u♫203(♫>
【问题讨论】:
-
什么是“奇怪的输出”?
-
这不是你的问题的原因,但不是
getData(&barcode, &warehouse, &product, &qualifier);,而是写getData(barcode, warehouse, product, qualifier);更常见 -
对不起,我没有想清楚。我现在将添加奇怪的输出!
标签: c arrays function pointers