Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在C中创建一个字符指针数组吗?_C_Arrays_Multidimensional Array - Fatal编程技术网

我可以在C中创建一个字符指针数组吗?

我可以在C中创建一个字符指针数组吗?,c,arrays,multidimensional-array,C,Arrays,Multidimensional Array,我是C语言的新手,C语言与我所学的任何其他语言都不一样。在我的家庭作业中,我想创建一个指向一个字符数组的字符数组,但不是创建一个多维字符数组,我想我应该有更多的控制权,创建字符数组,并将每个单独的字符数组放入原始字符数组的索引中: char keywords[10]; keywords[0] = "float"; 上面的例子是为了澄清一个简单的案例。但我的问题是由于我一直在做的研究,我对一些事情感到困惑。通常,这在其他语言中也适用,但在C语言中则是: char *keyword[10]; ke

我是C语言的新手,C语言与我所学的任何其他语言都不一样。在我的家庭作业中,我想创建一个指向一个字符数组的字符数组,但不是创建一个多维字符数组,我想我应该有更多的控制权,创建字符数组,并将每个单独的字符数组放入原始字符数组的索引中:

char keywords[10];
keywords[0] = "float";
上面的例子是为了澄清一个简单的案例。但我的问题是由于我一直在做的研究,我对一些事情感到困惑。通常,这在其他语言中也适用,但在C语言中则是:

char *keyword[10];
keywords[0] = "float";
但当我想通过函数发送时,为什么需要这样做:

void function(char **keyword); //function prototype
仅仅传递数组指针就足够了吗

char *keyword[10];
关键字
字符*
的数组10。在值上下文中,它转换为指向
char*
的指针

这种转换是Chris Torek称之为“规则”的一部分:

正如其他地方所指出的,C有一条关于数组和指针的非常重要的规则。这条规则——规则——表示,在值上下文中,“array of T”类型的对象成为“pointer to T”类型的值,指向该数组的第一个元素

有关更多信息,请参见此处:

C-FAQ中还有一个关于数组到指针转换的条目:

问题6.3:那么C语言中“指针和数组的等价性”是什么意思呢


在C中,您不能真正地将数组传递给函数。相反,您传递一个指向数组开头的指针。由于您有
char*
数组,函数将获得一个指向
char*
的指针,即
char**

如果需要,可以(在原型中)编写
char*keyword[]
而不是
char**keyword
。编译器将自动转换它

此外,在C语言中,您可以取消对数组之类的指针的引用,因此“转换为指针”几乎没有任何损失

void function(char **keyword);
Andy,想想数组只是一个指针(指向数组的开头),这就是为什么要写:

void function(char **keyword);
因为您已经创建了一个指向字符指针的数组

如果更容易理解,请尝试:

void function(char *keyword[]);

<>但是使用第一个C标准更高,但是如果你使用C++编译器就不重要了。

< P>看起来你被

中的双星迷惑了。
void function(char ** keyword);
双星只是意味着此函数期望您传递指向字符的指针。此语法不包含任何关于您正在使用数组或字符实际上是字符串中许多字符中的第一个字符的信息。这取决于作为程序员的您是否知道这是什么样的数据结构
char**
实际上指向

例如,假设数组的开头存储在地址0x1000处。函数的
关键字
参数的值应为0x1000。如果取消引用
关键字
,则会得到数组中的第一个条目,即指向字符串“float”中第一个字符的
字符*
“。如果取消引用
字符*
,则会得到字符“f”

这方面的(人为)代码如下所示:

void function(char **keyword)
{
    char * first_string = *keyword;   // *keyword is equivalent to keyword[0]
    char first_char = *first_string;  // *first_string is equivalent to first_string[0]
}
在上面的例子中有两个指针。通过在取消引用前向第一个指针添加偏移量,可以访问数组中的不同字符串。通过在取消引用第二个指针之前向其添加偏移量,可以访问字符串中的不同字符。

答案如下

#include<stdio.h>

int main(void)
{
        char *CharPtr[3];
        char a[4]="abc";
        char b[4]="def";
        char c[4]="ghi";
        CharPtr[0]=a;
        CharPtr[1]=b;
        CharPtr[2]=c;

        printf("\n content of CharPtr[0] =%s",CharPtr[0]);
        printf("\n content of CharPtr[1] =%s",CharPtr[1]);
        printf("\n content of CharPtr[2] =%s\n",CharPtr[2]);

        printf(" \n content of char a[4]=%s",a);
        printf(" \n content of char b[4]=%s",b);
        printf(" \n content of char c[4]=%s\n",c);
}
#包括
内部主(空)
{
char*CharPtr[3];
字符a[4]=“abc”;
字符b[4]=“def”;
字符c[4]=“ghi”;
CharPtr[0]=a;
CharPtr[1]=b;
CharPtr[2]=c;
printf(“\n CharPtr[0]的内容=%s”,CharPtr[0]);
printf(“\n CharPtr[1]的内容=%s”,CharPtr[1]);
printf(“\n CharPtr[2]的内容=%s\n”,CharPtr[2]);
printf(“\n字符a[4]=%s”,a的内容);
printf(“\n字符b[4]=%s”,b的内容);
printf(“\n字符c[4]的内容=%s\n”,c);
}

char*关键字[10]
是一个字符指针数组。所以
关键字[0]
关键字[1]
。。以此类推将有不同字符数组的地址

printf
中,您可以使用
%s
关键字[0]
打印其地址(即数组中第一个字节的地址)存储在
关键字[0]
的整个字符数组


在传递给函数时,如果您给出
*关键字
,则指的是位于(存储在
关键字[0]
的地址)的值,该值也是一个地址。因此,要获取值而不是地址,可以添加另一个
*
。。。希望能澄清一点。

我假设您正在分配第一个字符串:

"float"
到关键字[0]的第一个索引位置

char keyword[0] = "float";
哪个是数组的第一个索引位置:

char keyword[10];
如果是前一种情况,那么从某种意义上说,您实际上是在创建一个包含数据结构的数据结构。任何类型的数组都是C中该类型的“最小”数据结构。考虑到在您的示例中您正在创建一个字符数组,那么您实际上是在最小内置数据结构(数组)的每个索引位置使用最小的数据类型(char=1bit)

话虽如此,如果在您的示例中,您试图创建一个数组数组;你的角色数组

/* Hold ten characters total */
char keyword[10];  
可容纳10个字符。每个索引位置一个(您可能已经知道)。因此,在声明名为关键字的数组后,您可以尝试使用另一个(第二个)字符数组初始化数组的第一个索引位置:

/* I believe this is what you had stated */
char keywords[0] = "float";
第二个字符数组的索引大小为5个位置

为了实现您想要的目标,您需要创建一个基本上模拟“保存”其他数据结构的数据结构效果的数组

不是
char *keyword[10] = { 
                        char *keywords0[10] = {"float", etc, etc, etc.};
                        char *keywords1[10] = {"keyword1", "secondIndexOfThisArray", etc, etc, etc.}; 
                       and so

                   };
typedef *nestedDataStructures {

    struct keyWords[];
    struct keyWords1[];
    struct keyWords2[];

    ... and so on.
}; nestedDataStructures
/* Example of the program and declaration with out a function */
#include <stdio.h>

int main(){

    /* 
     * A character pointer array that contains multiple 
     * character arrays.
     */

    char *grewMe[2] = {"I want to ", "grow to be bigger"};

    int w = 0;

    for(; w < 2;) {
        printf("%s", grewMe[w]);
        ++w;
    }

    printf(" :-)\n");
    w = 0;
    return 0;
}
// Output:
// I want to grow to be bigger :-)
/* Example of program: function passed arguments 
 * of a pointer to the array of pointers 
 */
#include <stdio.h>

void mygrowth(char *growMe[]);

int main(){

    char *growMe[2] = {"I want to ", "grow to be bigger"};

    mygrowth(growMe);  
    printf(" :-)\n");

    return 0;

}
void mygrowth(char *growMe[])
{

    int w = 0;
    for (; w < 2;) {
        printf("%s", growMe[w]);
        ++w;
    } 
}
/* 
 * This program compiles, runs and outputs properly
 * Example of a program with a function of 
 * arguments pnt2pnter 
 */

#include <stdio.h>
#include <stdlib.h>

void thoughtAsAFunction(char **iThink);

int main()
{

    char *iThink[10] = {"I am trying to grow, but it's a hard task to ",
                        "accomplish. My father is short ", 
                        "my mother is even shorter than him, ", 
                        "what is the probability of me getting taller? ", 
                        "Well both my grandfather's were Six ",
                        "Foot Five, and both my grandmother's ", 
                        "were over 5 foot 8 inches tall! If my ",  
                        "grandparent's genes point to my parents, and my ", 
                        "parent's genes point to mine I might have a chance ",
                        "of being 6 foot. Do you know what I mean? "};

    thoughtAsAFunction(iThink);

    printf(":-)\n");

    return 0;
}
void thoughtAsAFunction(char **iThink) {

    int andy = 0;
    for (; andy < 10;) {
        char * pntThroughPnt = iThink[andy];
        printf("%s", pntThroughPnt);
        ++andy;
    }
    andy = 0;
}
/*
 * This program compiles, runs, and outputs all of the character
 * arrays. 
 * 
 */
#include <stdio.h>
#include <stdlib.h>

void thoughtAsAFunction(char **iThink);

int main()
{

    char *iThink[10] = {"I am trying to grow, but it's a hard task to ",
                        "accomplish. My father is short ", 
                        "my mother is even shorter than him, ", 
                        "what is the probability of me getting taller? ", 
                        "Well both my grandfather's were Six ",
                        "Foot Five, and both my grandmother's ", 
                        "were over 5 foot 8 inches tall! If my ",  
                        "grandparent's genes point to my parents, and my ", 
                        "parent's genes point to mine, then I might have a chance ",
                        "of being 6 foot. Do you know what I mean? "};

    int andy = 0;
    for (; andy < 10;) {
        // pass by reference and increment.
        thoughtAsAFunction(&iThink[andy]);
        ++andy;

    }

    printf(":-)\n");
    andy = 0;

    return 0;
}
void thoughtAsAFunction(char **iThink) {

    char * pntThroughPnt = *iThink;
    printf("%s", pntThroughPnt);

}