#include "usefunc.h" //don't worry about this -> lib I wrote
int main()
{
int i;
string given[4000], longest = "a"; //declared new typdef. equivalent to 2D char array
given[0] = "a";
printf("Please enter words separated by RETs...\n");
for (i = 1; i < 4000 && !StringEqual(given[i-1], "end"); i++)
{
given[i] = GetLine();
/*
if (sizeof(given[i]) > sizeof(longest))
{
longest = given[i];
}
*/
printf("%lu\n", sizeof(given[i])); //this ALWAYS RETURNS EIGHT!!!
}
printf("%s", longest);
}
Run Code Online (Sandbox Code Playgroud)
它为什么总是返回8 ???
我正在尝试学习C++函数模板.我传递一个数组作为指向我的函数模板的指针.在那,我试图找到一个数组的大小.这是我使用的功能模板.
template<typename T>
T* average( T *arr)
{
T *ansPtr,ans,sum = 0.0;
size_t sz = sizeof(arr)/sizeof(arr[0]);
cout<<"\nSz is "<<sz<<endl;
for(int i = 0;i < sz; i++)
{
sum = sum + arr[i];
}
ans = (sum/sz);
ansPtr = &ans;
return ansPtr;
}
Run Code Online (Sandbox Code Playgroud)
即使我将指针传递给整数数组,该cout语句也会显示arras 的大小.现在我知道这可能是我之前提到的问题的可能重复,但我需要对此有更好的解释.15
我唯一能想到的是,因为模板是在运行时调用的,并且sizeof是一个编译时运算符,所以编译器只是忽略了这一行
int sz = sizeof(arr)/sizeof(arr[0]);
Run Code Online (Sandbox Code Playgroud)
因为在实际调用函数之前它不知道arr的确切类型.这是正确的还是我错过了这里的东西?将指针发送到函数模板也是可靠的吗?
我对此感到不安fread; 这是非常基础的,但我无法找到答案.
#include <stdio.h>
#include <stdlib.h>
void main ()
{
int i;
FILE *fp;
char *re;
fp = fopen("/net2/192.168.42.151/stud3/2013/dobo1298/data.txt","r");
i = sizeof(char);
printf("%d",i);
re = calloc(10,i);
fread(re,i,1,fp);
printf("%s",*re);
}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main(){
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d\t",sizeof('a'));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译我的代码时,输出将是:"842".有人可以解释为什么我得到这个输出?
# include <stdio.h>
int main()
{
int a = sizeof("string");
printf("%d", a );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码打印7作为输出,而字符串的长度只有6.有人可以解释一下吗?
我有这个c ++结构:
struct Packet
{
uint32 MessageCount;
uint32 Length;
uint32 FieldValue;
union PacketHeader
{
uint32 typeInfo;
struct MagicVersion
{
uint8 MagicNumber[3];
uint8 Version;
};
};
Data * Payload(void) { return reinterpret_cast< Data * >(this + 1U); }
Data const * Payload(void) const { return reinterpret_cast< Data const * >(this + 1U); }
Packet * nextPacket(void) { return reinterpret_cast< Packet * >(this + 1U) + Length; }
Packet const * nextPacket(void) const { return reinterpret_cast< Packet const * >(this + 1U) …Run Code Online (Sandbox Code Playgroud) 在什么基础上,类对象的大小显示为12?
class testvector
{
public : vector<int> test;
};
int main()
{
testvector otestvector;
cout<<"size :"<<sizeof(otestvector)<<"\n";
cout<<"size of int :"<<sizeof(int);
}
Run Code Online (Sandbox Code Playgroud)
输出:
size :12
size of int :4
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct _person
{
char *fname;
char *lname;
bool isavailable;
}Person;
Person *getPersonInstance(void)
{
Person *newPerson = (Person*) malloc(sizeof(Person));
if(newPerson == NULL)
return NULL;
return newPerson;
}
void initializePerson(Person *person, char *fname, char *lname, bool isavailable)
{
person->fname = (char*) malloc(strlen(fname)+1);
/*problematic behaviour if i write: person->fname = (char*) malloc (sizeof(strlen(fname)+1)); */
person->lname = (char*) malloc(strlen(lname)+1);
/*problematic behaviour if i write: person->lname = (char*) malloc (sizeof(strlen(lname)+1)); */
strcpy(person->fname,fname);
strcpy(person->lname,lname);
person->isavailable = isavailable; …Run Code Online (Sandbox Code Playgroud) 根据这个问题,sizeof(true)或者sizeof(false)是4字节,因为true它false是宏并在#include <stdbool.h>头文件中定义.
但是,这里有趣的程序输出.
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool a = true;
printf("%zu\n", sizeof(a ? true : false, a));
/* ^^ -> expresion2 */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出(现场演示):
1
Run Code Online (Sandbox Code Playgroud)
C11 6.5.15条件运算符(P4):
第一个操作数被评估; 在其评估与第二或第三操作数的评估之间存在一个序列点(以评估者为准).仅当第一个操作数不等于0时才评估第二个操作数; 仅当第一个操作数比较等于0时才评估第三个操作数; 结果是第二个或第三个操作数的值(无论哪个被评估),转换为下面描述的类型.110)
这里a是一个bool类型和赋值true,然后在sizeof运算符内,运算conditional符执行expression2因为a是a true.
因此,comma运算符部分(表达式3)未根据标准进行评估,而表达式2是宏.所以,根据那个问题,宏的输出是4字节,但在这里,程序的输出是1字节.
那么,为什么 只sizeof(a ? …
我有这个问题我不明白为什么我得到不正确的结果:
if(sizeof(int) > -1)
cout << "true" << endl;
else
cout << "false" << endl;
Run Code Online (Sandbox Code Playgroud)
上面也总是打印错误!但下面是正确的:
int n = sizeof(int);
// correct
if(n > -1)
cout << "true" << endl;
else
cout << "false" << endl;
Run Code Online (Sandbox Code Playgroud)
谁能向我解释发生了什么?谢谢.