简体   繁体   English

C- 声明字符数组

[英]C- Declaring char arrays

I'm new to C and am having trouble declaring a character array for a class project.我是 C 新手,在为类项目声明字符数组时遇到问题。 Here's roughly what I'm doing:这大致是我在做什么:

char test[]="Test\0";

char *Pointer;
Pointer=test;

I then have a function printString(char* chars) that takes Pointer as an argument.然后我有一个函数printString(char* chars)将 Pointer 作为参数。 When I try to compile, I'm told that neither test or Pointer are declared.当我尝试编译时,我被告知既没有声明 test 也没有声明 Pointer。 I'm not sure why, so can someone point me in the right direction.我不知道为什么,所以有人可以指出我正确的方向。

This is the whole code:这是整个代码:

main()
{

   char *test2="Test\0";

    printString(test2);
}

printString(char* charArray)
{

    int charPos=0;
    int endOfString=1;

    char al;
    char ah;
    int ax;

    while(endOfString==1)
    {
            al=charArray[charPos];
            ah=0xE;
            ax=ah*256+al;

            interrupt(0x10,ax,0,0,0);


            if(al=='\0')
            {
                    return 0;
            }

            charPos++;
    }
}

First of all, having the NULL character is not necessary there.首先,没有必要在那里使用 NULL 字符。

Here is why:原因如下:

When an array of characters is declared and it is initialize, like in your example:当一个字符数组被声明并被初始化时,就像在你的例子中一样:

char test[] = "Test";

The compiler will put the characters from "Test" in the test array, then add a null character so that test can be used as a string like so:编译器会将“Test”中的字符放入 test 数组中,然后添加一个空字符,以便 test 可以用作字符串,如下所示:

    +---+---+---+---+----+
test| T | e | s | t | \0 |
    +---+---+---+---+----+

In regards to your question, see if this helps:关于你的问题,看看这是否有帮助:

void print_string( char *p ) {
    while ( *p != '\0' ) {
     printf( "%c", *p );
     p++;
    }

}

Remember a C-style string is a sequence of characters and it's always terminated with NULL character.请记住,C 风格的字符串是一个字符序列,它总是以 NULL 字符结尾。

The function "print_string", for example, expects a pointer to a char as an argument ( you can past the char array you created, and it will work since arrays are treated as pointers. ) The function will print each character until the NULL character is encountered.例如,函数“print_string”需要一个指向 char 的指针作为参数(您可以传递您创建的 char 数组,它会起作用,因为数组被视为指针。)该函数将打印每个字符,直到 NULL 字符遇到了。

IMPORTANT:重要的:

char test[]  = "Test";
char *test_2 = "Test";

In the array version, the characters stored in test can be modified, like elements of any array.在数组版本中,可以修改存储在 test 中的字符,就像任何数组的元素一样。 In the pointer version, test points to a string literal, and string literals should NOT be modified.在指针版本中,测试指向字符串文字,不应修改字符串文字。

I believe the problem might be because of that, you are trying to modified a string literal.我相信问题可能是因为这个原因,您正在尝试修改字符串文字。 Doing so causes undefined behavior.这样做会导致未定义的行为。

Either use an array or allocate memory and make char pointer point to it.使用数组或分配内存并使 char 指针指向它。

For use your function: printString(char* chars).使用你的函数:printString(char* chars)。

declare just:只声明:

char *my_string = "Test";
printString(my_string);

The problem is possibly because you don't declare the function before you use it.问题可能是因为您在使用该函数之前没有声明它。 In C you really have to declare everything before it's used.在 C 中,您确实必须在使用之前声明所有内容。 In this case do eg在这种情况下做例如

void printString(char* charArray);

int main(void)
{
    ...
}

void printString(char* charArray)
{
    ...
}

Sorry... but wtf ?对不起...但是wtf? lol哈哈

   main()
    {

       char *test2="Test\0";

        printString(test2);
    }

    printString(char* charArray)
    {

        int charPos=0;
        int endOfString=1;

        char al;
        char ah;
        int ax;

        while(endOfString==1)
        {
                al=charArray[charPos];
                ah=0xE;
                ax=ah*256+al;

                interrupt(0x10,ax,0,0,0);


                if(al=='\0')
                {
                        return 0;
                }

                charPos++;
        }
    }

for print a string, just use that:要打印一个字符串,只需使用它:

#include <stdio.h>
int  main(void)
{
   char  *my_string = "Test";

   printf("%s", my_string);
   return (0);
}

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

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