【发布时间】:2012-11-01 00:09:22
【问题描述】:
我在这里做错了什么,将 char 数组传递给函数并在函数中提供每个索引内存(使用 malloc()),然后使用 gets() 从键盘插入一些东西。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
void test(char *arr[]);
int main(){
char *arr[2];//2 is the rows
/* arr[0] = malloc(80);//This commented code works
arr[1] = malloc(80);
strcpy(arr[0], "hey");
strcpy(arr[1], "whats up");
*/
test(*arr);
printf("in array[0]: %s", arr[0]);
printf("in array[1]: %s", arr[1]);
return 0;
}
void test(char *arr[]){
int index;
char *input = malloc(80);
for(index = 0; index < 2; index++){
arr[index] = malloc(80);
gets(input);
strcpy(arr[index], input);
//arr[0] = input;
}
}
只是一个非常基本的程序,由于某种原因我遇到了麻烦。还有一个问题当我声明一个数组时,这些表单之间有什么区别
char *array
反对
char *array[size]
或
char **array
谢谢, 凯文
【问题讨论】: