Possible Duplicate:
Sizeof an array in the C programming language?
I am passing a char
array from a function say f1 to another function f2. In function f1 I am printing its size using sizeof
& it results in 9
. In the second function I am again printing its size using the same statement as above but this time it results in 8
. And infact I am not using this array in between printing the two values. When I tried to run the same code on a different laptop, for the second function it results 4
.
It is clear from this question that why I am getting 4, but why I am getting 8 on another laptop.
Why this is happening?
My whole code is too big so I am sharing only the essential part:
(Here the array about which I am talking is plid
and I am calling the function login
from the first function. I am not sharing the complete functions because of their length. logp, errorp..
these are my own written functions that are writing to a file, which I have shared in the end.)
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);
}
logfile output:
__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