简体   繁体   中英

strcat with char pointers

So, my problem is simple, I whant to create a function to separate the words in the file and parse those words and create an special think for those words, but, I have an error in strcat, when I try to separate the tokens:

look the code:

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

using namespace std;

bool compile(FILE *fp)
{
    char c;
    char token[256];
    bool _return = false;

    while ((c = getc(fp)) != EOF)
    {
        if(isalpha(c))
        {
                    // The error is here
            strcat(token, (char*)c);
            printf("%c\n", c);
        }
        else
        {
            printf("----> %c\n", c);
        }
    }

    printf("%s\n", token);

    return _return;
}

int main()
{
    char *filename = "test.in";

    FILE *fp = fopen(filename, "rb");
    if(!fp)
    {
        return 1;
    }

    if(!compile(fp))
    {
        printf("ERROR...\n");
    }
    fclose(fp);
}

THe code builds normally, but, when I debug, the debugger goes to strcat.asm file :( So, where is my error, thanks for the help. (:

-0-0-0-0-0-0-0-0-0-> edit And, when I change the strcat to:

strcat(token, &c)

I receive this error from the debugger:

Unhandled exception at 0x20E801F2 in APP.exe: 0xC0000005: Access violation (parameters: 0x00000008).

This is wrong:

strcat(token, (char *) c);

The variable c isn't a string, it's only a single character. The cast to (char *) is just masking a compiler error.

If you want to add characters to a string one at a time,

std::size_t pos = 0;
char buf[256];

// ...

if (isalpha(c)) {
    assert(pos < sizeof(buf));
    buf[pos++] = c;
}

// ...

assert(pos < sizeof(buf));
buf[pos] = '\0';
printf("buf = %s\n", buf);

Or, you know, do it the easy way:

#include <string>

std::string s;

// ...

if (isalpha(c)) {
    s += c;
}

// ...

printf("buf = %s\n", s.c_str());

Don't bother to debug the insides of strcat() . If your program crashes inside strcat() , it is an error in the calling function. (It's usually an error to even call strcat() at all.)

strcat expects a null-terminated string; you cannot pass a pointer to an individual character and expect it to work correctly.

Replace

strcat(token, (char*)c);

with

char tmp[2];
tmp[0] = c;
tmp[1] = '\0';
strcat(token, tmp);

to make it work.

You also need to set token[0] to zero to avoid undefined behavior due to it not being initialized.

When you cast c to char * , you're turning whatever value was in the character (say 100 in hexadecimal) into a memory address ( 0x100 ) and then looking for a string at that memory address. No wonder it doesn't work! You'll want to turn the character into a string before popping it into strcat .

Three things:

  • You don't check if you overflow the 256 characters in your buffer
  • When you convert char c to (char *) c you are saying that c is a memory address. In actuality, it is a value. What you probably want is a reference to the character, &c
  • You need to add a null terminator to the end of what you are concatenating.

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