将多个字符串传递给函数C. [英] Pass multiple strings to function C

查看:72
本文介绍了将多个字符串传递给函数C.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C的基础知识和原理。

我现在来到指针,字符串和结构。

现在我正在处理这段代码以通过数组的内容到函数。

我有这个代码将不同数组的内容传递给函数。

我成功完成的是:



1.如何传递一个完整的字符串,因为它被认为是数组的一个元素。

2.如何传递char和int数组。



我现在遇到的问题:



1.如何将多个字符串的数组传递给函数。

2.如何指定数组指针以将它们传递给函数。



这是我的代码到目前为止:

有评论的部分来展示我在代码中所做的工作,我实际上是C的初学者。我觉得留下排列的评论行是很好的,这样读者就能更好地理解我的问题。





I'm learning the basics and principles of C.
I came now to pointers, strings and structs.
Now I'm working on this code to pass arrays' content to functions.
I have this code to pass content of different arrays to function.
What I succeeded to accomplish is:

1.How to pass one complete string because it's considered as one element of the array.
2.How to pass array of char and ints.

The issues I have now:

1.How to pass arrays of multiple strings to functions.
2.How to assign a pointer to the arrays to pass them also to functions.

This is my code so far:
There are commented parts to show the work I done in the code, I'm actually a beginner in C. I just feel that it's nice to leave arranged commented lines so the readers understand my issue more.


#include<stdio.h>
#include<string.h>
#include<stdint.h>

    void print_array(char *arr,int8_t cnt);
    void print_array(char *arr,int8_t cnt)
    {
        int_fast8_t i;
        printf("Number of elements is: %d\n",cnt);
        for (i=0;i<cnt;i++)>
        {
            printf("Elements of array: %s\n",arr);
        }
    }

    void print_len (char *arr,int8_t cnt);

    void print_len (char *arr,int8_t cnt)
    {
        char i,l=strlen(arr);
            for (i=0;i<cnt;i++)>
        {
            printf ("%d\n",strlen(arr));
        }
    }


int main()
{

    char *array_5 [] = {"mm","End of Multiple Strings Array","simple bluetooth connection",
    "datalogging purposes and accessing recorded data","THE OPERATING ENVIRONMENT"};

    int8_t cnt5 = sizeof(array_5)/sizeof(array_5[0]);
    int8_t len5;

    print_len(*array_5,cnt5);
    print_array(*array_5,cnt5);
    return 0;
}
pre>

What I have tried:

Pass multiple strings array pointer to a function, with name of the array as a start and then I think to develop array of arrays pointers to pass them consequently to a function pointer.

I have changed the asterisk in the function call, function declaration, definition and printf of the array pointer.</stdint.h></string.h></stdio.h>

推荐答案

一些事情,首先如果你打算这样做,

A few things, first of all if you are going to do this,
void print_array(char *arr,int8_t cnt);
void print_array(char *arr,int8_t cnt) {}



最好只做后一个,删除声明语句并保持定义(和声明)仅。其次,因为您已经知道如何传递字符串(这是字符数组)。像这样,


It is better to just do the later one, remove the declaration statement and keep the definition (and declaration) on only. Secondly, since you already know how to pass strings (which are array of characters). Like this,

print_array(char *arr, int8_t cnt) { }



是什么阻止你这样做?


What stops you from doing this?

print_arrays(char **arr, int8_t cnt) { } // cnt won't make much sense now.



请注意,我使用了指向指针的指针 [ ^ ]。这意味着,或者至少我可以说,在C语言上下文中,是一个数组数组。您可以在其上执行以下操作,然后,


Notice that I used a pointer to pointer[^] here. Which means, or at least I can say, in the C language context, an array of array. You can do the following on it, then,

char **array = { "Afzaal", "Ahmad", "Zeeshan" };
// print_arrays(array, 3);



最后,我实际上没有得到你想要的部分将指针分配给数组,因为你实际上得到指向数组的指针 。但是,这将使我们进入C ++领域,其中支持引用,并且据我所知,C不支持pass-by-reference,只传递值,这正是C函数看起来的原因像这样,


Finally, I didn't actually get the part where you wanted to assign the pointer to the array, because you actually get the pointer to the array. However, that would now take us in the realm of C++ where references are supported and as far as I can conclude, C doesn't support pass-by-reference, only pass-by-value and that is exactly why the C functions look like this,

void func(char *obj) {
   // Do something
}

int main() {
   int a = 25;
   func(&a);
} 



我会让你继续学习,不断尝试。我个人的建议是使用GCC编译器。这是一个很好的,我个人喜欢它。



在C和C ++中通过引用传递价值 - C ++论坛 [ ^ ]

C中的字符串 [ ^ ]

C Strings - Cprogramming.com [ ^ ]


I would move you onwards to learn something, keep trying. My personal recommendation is to use GCC compiler. That is a good one and I personally like it.

Passing value by reference in C and C++ - C++ Forum[^]
Strings in C[^]
C Strings - Cprogramming.com[^]


非常感谢您的回答,我昨晚因为正在搜索并查看有关此特定操作的练习或示例的电子书而感到害怕。我不能,然后今天早上我告诉了培训师,他马上解决了!这很简单。



Thank you very much for the answer, I freaked out last night because I was searching and looking into the eBooks for the exercise or example about this specific action. And I couldn't, then this morning I told the trainer about it and he solve it right away! It's very simple.

    void print_array(char *arr[],int8_t cnt);
    void print_array(char *arr[],int8_t cnt)
    {
        int8_t i;
        printf("Number of elements is: %d\n",cnt);
        for (i=0;i<cnt;i++)
        {
            printf("Elements of array: %s\n",arr[i]);
        }
    }

print_array(array_5,cnt5);


这篇关于将多个字符串传递给函数C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆