C语言 如何在C中的数组中存储单词?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34925792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:19:17  来源:igfitidea点击:

How to store words in an array in C?

carraysstring

提问by Vedant Kashyap

I want to store an array of words in C

我想在 C 中存储一组单词

To be more specific i want to form an array like this:

更具体地说,我想形成一个这样的数组:

a_cool_array_name={"first","second","third","fourth","fifth"};

i tried using:

我尝试使用:

#include <stdio.h>

int main()
{
    /*i have tried using both single and double quotation marks */

    char array1[4];
    array1[0]='hello';
    array1[1]='world';
    array1[2]='its';
    array1[3]='me';
    printf("%s",array1[0]);
}

I have tried using both single and double quotations however BOTHfail

我一直在使用单和双引号然而试图BOTH失败

In the single quotations the program crashes the moment i try to build and run it. In case of double quotation marks the program doesn't even compile at all. It throws up an error which looks something like this

在单引号中,程序在我尝试构建和运行它的那一刻崩溃。在双引号的情况下,程序甚至根本无法编译。它抛出一个错误,看起来像这样

8:11: error: assignment makes integer from pointer without a cast

8:11:错误:赋值从指针生成整数而不进行强制转换

Whats the difference in using single and double quotations in strings and such?
AND
How do i make an array of words?

在字符串等中使用单引号和双引号有什么区别?
AND
我如何制作一组单词?

Is there any other way of storing words other 2D array? which pretty messes some things up... :(

有没有其他方法可以存储其他二维数组的单词?这把一些事情搞砸了...... :(

回答by Aleksandar Makragi?

  1. You use single quotation marks for single chars: 'c' 'd'etc, and you use double quotation marks for strings like "first".

  2. What you want to use here is matrix, or 2 dimensional array of chars:

    char array[10][20];
    
  1. 您对单个字符使用单引号:'c' 'd'等,对像"first".

  2. 你想在这里使用的是矩阵,或字符的二维数组:

    char array[10][20];
    

And now in array[0]is array of 20 characters. You can use this as you would normal array. For example: array[0][0]in code below gives you 'f'.

现在 inarray[0]是 20 个字符的数组。您可以像使用普通数组一样使用它。例如:array[0][0]在下面的代码中为您提供'f'.

char array[][20] = {"first","second","third","fourth","fifth"};

And now:

现在:

printf("%s\n",array[0]);would give you "first"

printf("%s\n",array[0]);会给你“第一”

回答by Iharob Al Asimi

You almost had it, to define an array of strings you can do this1

你几乎拥有它,定义一个字符串数组,你可以这样做1

const char *array[] = {"first", "second", "third", "fourth", "fifth"};

Also please consider:

还请考虑:

  1. The single quotes are used to get an intthat corresponds to the ascii value of a single character and is called a character constant. Multiple characeters enclosed by single quotes are called multicharaceter constantsand also give you an intvalue, the value being implementation defined.

  2. When using string literals like the ones initializing the array above, always use const. That will help you prevent modifying them which is illegal.

  1. 单引号用来得到int对应于单个字符的ascii值的一个,称为字符常量。由单引号括起来的多个字符称为字符常量,并且还为您提供一个int值,该值是implementation defined

  2. 当使用像上面初始化数组那样的字符串文字时,总是使用const. 这将帮助您防止修改它们是非法的。

There is no stringtype in c. To have strings you need to understand what they are and how they work. A cstring is nothing but a sequence (array if you will) of non-\0bytes followed by a '\0'that marks the end of it.

c 中没有字符串类型。要拥有字符串,您需要了解它们是什么以及它们是如何工作的。甲Ç串不过是一个序列(数组,如果你会)的非字节之后是该标记它的结束。\0'\0'

In the code above you have 5 strings, take for example "first", it consists of the following sequence

在上面的代码中,您有 5 个字符串,例如"first",它由以下序列组成

{'f', 'i', 'r', 's', 't', '
#include <stdio.h>

int main()
{
    const char *array1[4]; // use const because we're pointing to literals

    array1[0] = "hello";
    array1[1] = "world";
    array1[2] = "its";
    array1[3] = "me";

    printf("%s %s %s %s\n", array1[0], array1[1], array1[2], array1[3]);
}
'}

the terminating '\0'is very important.

终止'\0'是非常重要的。

So you create an array of pointers to strings, but strings in the sense explained above.

所以你创建了一个指向strings的指针数组,但是上面解释的字符串。



1An array of charpoitners, initialized with string literals. So it's an array of charpointers pointing to string literals.

1指针数组char,用字符串文字初始化。所以它是一个char指向字符串文字的指针数组。

回答by Marc Khadpe

In C, text strings are stored as arrays of characters. If you are dealing with strings of variable length, you can use pointers to refer to them. Here's how to do it for your example.

在 C 中,文本字符串存储为字符数组。如果您正在处理可变长度的字符串,则可以使用指针来引用它们。以下是如何为您的示例执行此操作。

main()
{

int i;
char item[30][10]={"Chickpeas","Rice","Semolina","Wheat","Almond","Sapota","Dates","Asafetida","Garlic","Chilly","##代码##"};

int amt[10];

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

printf("Please enter the price for %s:", item[i]);

scanf("%d",&amt[i]);

}

printf("\n\n\nAmount per item is ");

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

    printf("%s: %d\n",item[i],amt[i]);

}

dscnt=0;
getch();


}

array1is an array of 4 pointers to strings. Each pointer is initialized to point to a different literal string. Use double quotes (not single quotes) to create literal strings in your code.

array1是一个由 4 个指向字符串的指针组成的数组。每个指针都被初始化为指向不同的文字字符串。使用双引号(而不是单引号)在代码中创建文字字符串。

回答by Ashim Nath

Here is an example, hope this will help you:

举个例子,希望对你有帮助:

##代码##