分配:编写一个假设UPC代码是音频/视频产品的函数,并返回一个字符串,指示它是哪个产品。这是我必须为本实验编写的最后一个函数,我无法弄清楚如何从该函数返回一个字符数组。条形码阵列是用户输入的12位upc代码,阵列中的10个空格表示它是哪种类型的媒体。检查数字后,我想将指示的字符数组返回到主要打印。
char *getAudioVideoProductString(int barcodeArray[12]);
int main()
{
int barcodeArray[12];
do{
printf("\nEnter UPC code: ");
for(i = 0; i < 12; i++)
{
scanf("%d", &barcodeArray[i]);
}
avstring = getAudioVideoProductString(barcodeArray);
for(a = 0; a != '\0'; a++)
{
printf("%c", avstring[a]);
}
printf("\nAdd another UPC code [1 = add, 0 = quit]\n");
scanf("%d",&end);
}while(end !=0);
return EXIT_SUCCESS;
}
char *getAudioVideoProductString(int barcodeArray[12])
{
int i;
if (barcodeArray[10] == 1)
{
char b[] = "12\"\ LP or Single";
int lengthb = strlen(b);
char a[] = malloc(sizeof(char)*100);
for (i = 0; i < lengthb; i++)
{
a[i] = b[i];
}
}
if (barcodeArray[10] == 0| barcodeArray[10] == 3| barcodeArray[10] == 6)
{
char b[] = "Unassigned Audio Format";
int lengthb = strlen(b);
char a[] = malloc(sizeof(char)*100);
for (i = 0; i < lengthb; i++)
{
a[i] = b[i];
}
}
return a;
}
编译程序时,我遇到了这些错误:
答案 0 :(得分:-1)
char a[] = malloc(sizeof(char)*100); //-<< wrong a is not the pointer only the array
char *a = malloc(sizeof(char)*100); // -<< correct. you need the pointer to assign the address of the allocated memory
我认为你需要阅读一些有关数组和指针的内容。这里有很多关于它们的主题。只需搜索。
PS 如果你需要数组:
char a[100];