简体   繁体   中英

C- Declaring char arrays

I'm new to C and am having trouble declaring a character array for a class project. 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. When I try to compile, I'm told that neither test or Pointer are declared. 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.

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| 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.

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.

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. 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.

For use your function: 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. In this case do eg

void printString(char* charArray);

int main(void)
{
    ...
}

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

Sorry... but 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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