c - 传递给 C 中的函数时,char 数组的大小发生变化

标签 c arrays function

<分区>

Possible Duplicate:
Sizeof an array in the C programming language?

我正在将一个 char 数组从函数 say f1 传递到另一个函数 f2。在函数 f1 中,我使用 sizeof 打印它的大小,结果是 9。在第二个函数中,我再次使用与上面相同的语句打印它的大小,但这次它的结果是 8。事实上,我并没有在打印这两个值之间使用这个数组。当我尝试在另一台笔记本电脑上运行相同的代码时,第二个函数的结果是 4

由此可见question这就是为什么我得到 4,但为什么我在另一台笔记本电脑上得到 8。

为什么会这样?

我的整个代码太大了,所以我只分享最重要的部分: (我在这里谈论的数组是 plid,我从第一个函数调用函数 login。由于它们的长度,我没有分享完整的函数。logp, errorp..这些是我自己写的写入文件的函数,最后分享给大家。)

f1:

char choice[1], plid[9];
int choice_len = sizeof(choice);//this is importnat becz recvall takes ppointer to int
int ret;

logp(identity,0,0,"receiving the first choice of the client");
if(recvall(fd, choice, &choice_len, 0) != 0){ //whether want to play(login), or as audience(no login)
    logp(identity,0,0,"Error receiving the first choice of the client");
}

logp(identity,0,0,"Entering the choice Select switch");
switch(choice[0])
{
    case 'a':
        sprintf(buf, "plid_len(%d), username_len(), plid - %d",sizeof(plid), plid);
        logp(identity,0,0,buf);

        logp(identity,0,0,"User entered the choice 'a' and calling login");
        if( (ret = login(fd, plid)) == 0){
            sprintf(buf,"Player id is %s and Contacting player",plid);
            logp(identity,0,0,buf);
            contactPlayer( plid, fd);
            logp(identity,0,0,"Contacted To player succesfully");
        }else{

f2:

int login(int fd, char* plid){
char loginInfo[25], username[9], password[16];
int loginInfo_len = sizeof(loginInfo);
int ret;

char identity[IDENTITY_SIZE], buf[100];
sprintf(identity, "DISPATCHER-login-fd: %d -", fd);

sprintf(buf, "plid_len(%d), username_len(%d), plid - %d",sizeof(plid), sizeof(username), plid);
logp(identity,0,0,buf);


logp(identity,0,0,"Calling recvall to recv login credentials");
if ((ret = recvall(fd, loginInfo, &loginInfo_len, 0)) != 0) {
    errorp(identity,0,0,"Unable to recv login credentials");
    debugp(identity,1,errno,NULL);
}

日志文件输出:

__LOG__    |  Wed Dec 26 19:21:34 2012  |  Where - DISPATCHER-SocketHandler-fd: 6 -  |  LogMsg - Entering the choice Select switch
__LOG__    |  Wed Dec 26 19:21:34 2012  |  Where - DISPATCHER-SocketHandler-fd: 6 -  |  LogMsg - plid_len(9), username_len(), plid - -1314939296
__LOG__    |  Wed Dec 26 19:21:34 2012  |  Where - DISPATCHER-SocketHandler-fd: 6 -  |  LogMsg - User entered the choice 'a' and calling login
__LOG__    |  Wed Dec 26 19:21:34 2012  |  Where - DISPATCHER-login-fd: 6 -  |  LogMsg - plid_len(8), username_len(9), plid - -1314939296
__LOG__    |  Wed Dec 26 19:21:34 2012  |  Where - DISPATCHER-login-fd: 6 -  |  LogMsg - Calling recvall to recv login credentials
__LOG__    |  Wed Dec 26 19:21:34 2012  |  Where - access-recvall  |  LogMsg - Successfully recved the complete data

最佳答案

当您将数组传递给函数时,它会衰减为指向其第一个元素的指针。
当您在接收参数的函数内应用 sizeof 时,您会得到指针的大小(在该环境中)而不是数组的大小。

如果您需要函数内部数组的大小,只需将其作为附加函数参数传递即可。

关于c - 传递给 C 中的函数时,char 数组的大小发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14042009/

相关文章:

c - 为什么 uint8_t 和 uint16_t 的格式说明符相同 (%u)?

当我输入简单的 int 变量时崩溃

javascript - 从 API 结果覆盖索引数组

JavaScript 无法读取 function() 中的变量

c - 为什么这个类似的函数不会抛出相同的错误?

python - 如何在 python 中一起传递默认和可变长度参数?

c - 32位Linux上 "_Jv_RegisterClasses"和 "clock_gettime"在哪里定义的?

python - 合并 MIDI 和 Wav 文件

javascript - 如何迭代数组并将结果存储在单个对象或变量中?

php - 用多次出现的单词完成一个句子