C 中用户输入的指针指针字符串 (char **)
pointer pointer string (char **) from user input in C
我是 C 的新手,我似乎找不到太多关于指针指针 char 的信息来满足我的需要。这是我的简化代码:
int total, tempX = 0;
printf("Input total people:\n");fflush(stdout);
scanf("%d",&total);
char **nAmer = (char**) malloc(total* sizeof(char));
double *nUmer = (double*) malloc(total* sizeof(double));;
printf("input their name and number:\n");fflush(stdout);
for (tempX = 0;tempX < total; tempX++){
scanf("%20s %lf", *nAmer + tempX, nUmer + tempX); //I know it's (either) this
}
printf("Let me read that back:\n");
for (tempX = 0; tempX < total; tempX++){
printf("Name: %s Number: %lf\n",*(nAmer + tempX), *(nUmer + tempX)); //andor this
}
我不确定获取用户输入时指针 pointer char 的正确格式是什么。如您所见,我正在尝试获取人员姓名及其号码的列表。我知道数组、矩阵和类似的东西很容易,但它必须只是一个指针指针。谢谢!
如果要存储 N 个字符串,每个字符串最多 20 个字符,则不仅需要为指向字符串的指针分配 space,还需要分配 space自己拉弦。这是一个例子:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char ** argv)
{
int total, tempX = 0;
printf("Input total people:\n");fflush(stdout);
scanf("%d",&total);
printf("You entered: %i\n", total);
// note: changed to total*sizeof(char*) since we need to allocate (total) char*'s, not just (total) chars.
char **nAmer = (char**) malloc(total * sizeof(char*));
for (tempX=0; tempX<total; tempX++)
{
nAmer[tempX] = malloc(21); // for each string, allocate space for 20 chars plus 1 for a NUL-terminator byte
}
double *nUmer = (double*) malloc(total* sizeof(double));;
printf("input their name and number:\n");fflush(stdout);
for (tempX = 0; tempX<total; tempX++){
scanf("%20s %lf", nAmer[tempX], &nUmer[tempX]);
}
printf("Let me read that back:\n");
for (tempX = 0; tempX<total; tempX++){
printf("Name: %s Number: %lf\n", nAmer[tempX], nUmer[tempX]);
}
// Finally free all the things we allocated
// This isn't so important in this toy program, since we're about
// to exit anyway, but in a real program you'd need to do this to
// avoid memory leaks
for (tempX=0; tempX<total; tempX++)
{
free(nAmer[tempX]);
}
free(nAmer);
free(nUmer);
}
我是 C 的新手,我似乎找不到太多关于指针指针 char 的信息来满足我的需要。这是我的简化代码:
int total, tempX = 0;
printf("Input total people:\n");fflush(stdout);
scanf("%d",&total);
char **nAmer = (char**) malloc(total* sizeof(char));
double *nUmer = (double*) malloc(total* sizeof(double));;
printf("input their name and number:\n");fflush(stdout);
for (tempX = 0;tempX < total; tempX++){
scanf("%20s %lf", *nAmer + tempX, nUmer + tempX); //I know it's (either) this
}
printf("Let me read that back:\n");
for (tempX = 0; tempX < total; tempX++){
printf("Name: %s Number: %lf\n",*(nAmer + tempX), *(nUmer + tempX)); //andor this
}
我不确定获取用户输入时指针 pointer char 的正确格式是什么。如您所见,我正在尝试获取人员姓名及其号码的列表。我知道数组、矩阵和类似的东西很容易,但它必须只是一个指针指针。谢谢!
如果要存储 N 个字符串,每个字符串最多 20 个字符,则不仅需要为指向字符串的指针分配 space,还需要分配 space自己拉弦。这是一个例子:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char ** argv)
{
int total, tempX = 0;
printf("Input total people:\n");fflush(stdout);
scanf("%d",&total);
printf("You entered: %i\n", total);
// note: changed to total*sizeof(char*) since we need to allocate (total) char*'s, not just (total) chars.
char **nAmer = (char**) malloc(total * sizeof(char*));
for (tempX=0; tempX<total; tempX++)
{
nAmer[tempX] = malloc(21); // for each string, allocate space for 20 chars plus 1 for a NUL-terminator byte
}
double *nUmer = (double*) malloc(total* sizeof(double));;
printf("input their name and number:\n");fflush(stdout);
for (tempX = 0; tempX<total; tempX++){
scanf("%20s %lf", nAmer[tempX], &nUmer[tempX]);
}
printf("Let me read that back:\n");
for (tempX = 0; tempX<total; tempX++){
printf("Name: %s Number: %lf\n", nAmer[tempX], nUmer[tempX]);
}
// Finally free all the things we allocated
// This isn't so important in this toy program, since we're about
// to exit anyway, but in a real program you'd need to do this to
// avoid memory leaks
for (tempX=0; tempX<total; tempX++)
{
free(nAmer[tempX]);
}
free(nAmer);
free(nUmer);
}