1

So I am coding the skeleton for a larger program that will eventually be a message or IRC server.

The problem I am coming across is when trying to remove the end of line character from the input typed into to a character buffer by changing the value of '\n' to '\0'

The structure where the buffer is stored looks like this:

typedef struct{
    pthread_t tid; //the id of the thread
    char input[8192]; //message space of 2 to the 13th power
}thread_in;

thread_in user[3];

I then I made a temporary array pointer to my structure to make calling it easier.

char user_input[8192];
strncpy(user[num].input, user_input, 8192);

the problem is on the next line of code that give me the following warning:

Warning: assignment makes integer from pointer without a cast

This is the code:

user_input[strlen(user[num].input)-1] ="\0";

can someone point out why it thinks the assignment is to an integer since it is a array of characters.

4

2 回答 2

1

你想要'\0',没有"\0"。前者是单个字符,第二个是字符串文字。"\0"问题在于你的转换char;字符串文字衰减为指针并且char是整数类型。

于 2013-11-05T19:24:57.507 回答
1

"\0"是一个字符数组并衰减为一个指针。 '\0'是一个字符。所以在

user_input[strlen(user[num].input)-1] = "\0";

您正在尝试将指针(的第一个字符的地址"\0")存储在字符数组中。字符是一种整数类型,因此在尝试将指针放入字符数组时,您必须先将其转换为字符,并且由于字符是整数类型,这意味着将其转换为整数,因此会出现警告:

赋值从没有强制转换的指针生成整数

于 2013-11-05T19:25:25.937 回答