简体   繁体   English

如何通过将引用传递给C中的函数来获取输出字符串数组?

[英]How can I get my output string array by passing reference into a function in C?

When I am trying to get my output using the code in bellow, It's always stopped. 当我尝试使用下面的代码获取输出时,它总是停止。 But why and how can I solve this issue? 但是,为什么以及如何解决这个问题? Thanks in advance. 提前致谢。

#include<stdio.h>
void nameView(char* []);

int n, i, j;

int main(){

   char name[10][10];

    printf("Enter case: ");
    scanf("%d", &n);

    for(i=0; i<n; ++i){
        printf("Enter Name: ");
        scanf("%s", name[i]);
    }

    nameView(&name);


    return 0;
 }

  void nameView(char *b[]){

   for(i=0; i<n; ++i){

        printf("\n%s", *b[i]);
    }
 }

Programmed has stoped but when I replaced 编程已停止,但是当我更换时

    for(i=0; i<n; ++i){

        printf("\n%s", *b[i]);
    } 

Insead of "nameView(&name)" in main function it's work.How can I output by passing reference in "nameView()" function 在主函数中输入“ nameView(&name)”是可行的。如何通过在“ nameView()”函数中传递引用来输出

As was mentioned in the comments, your function prototype: 如评论中所述,您的函数原型:

void nameView(char* []);

Does not match the definition: 与定义不符:

void nameView(char *b){

Additionally, the argument you pass in ( &name which is `char (*)[10][10], ie a pointer to a 2D array) doesn't match either parameter. 另外,您传入的参数( &name是`char(*)[10] [10],即指向2D数组的指针)与任何一个参数都不匹配。

You need to declare and define your function like to take a 2D array of 10x10 chars : 您需要声明和定义函数,例如采用10x10 chars的2D数组:

void nameView(char b[10][10]){

Or equivalently, a pointer to an array of size 10: 或等效地,指向大小为10的数组的指针:

void nameView(char (*b)[10]){

And call it with the name of the array: 并使用数组名称进行调用:

nameView(name);

The following proposed code: 建议的代码如下:

  1. implements the corrections to the problems highlighted in the comments. 对注释中突出显示的问题进行更正。
  2. cleanly compiles 干净地编译
  3. performs the desired functionality 执行所需的功能
  4. checks for errors 检查错误
  5. eliminates the (very undesirable) global variables 消除(非常不希望的)全局变量
  6. limits the scope of the variables 限制变量的范围
  7. documents why each header file is included 说明为什么包含每个头文件

and now the code: 现在的代码:

#include <stdio.h>  // scanf(), printf(), perror()
#include <stdlib.h> // exit(), EXIT_FAILURE

#define MAX_NAMES  10
#define MAX_LENGTH 10


// prototypes
void nameView(char [][ MAX_LENGTH ], int);


int main( void )
{
    char name[ MAX_NAMES ][ MAX_LENGTH ];
    int n;

    printf("Enter case: ");
    if( 1 != scanf("%d", &n) )
    {
        perror( "scanf for number of names failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    EDIT:
    //if( 0 < n || n > MAX_NAMES )
    if( n < 0 || n > MAX_NAMES )
    {
        printf( "invalid entry for Name count, must be in the range 1...10\n" );
        exit( EXIT_FAILURE );
    }

    // implied else, 'n' contains a valid value

    for(int i=0; i<n; ++i)
    {
        printf("Enter Name: ");
        if( 1 != scanf("%9s", name[i]) )
        {
            perror( "scanf for name failed" );
            exit( EXIT_FAILURE );
        }
    }

    nameView(name, n);
    return 0;
} // end function: main


void nameView(char b[][ MAX_LENGTH ], int n)
{
    for(int i=0; i<n; ++i)
    {
        printf("\n%s", b[i]);
    }
} // end function: nameView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM