sizeof

Why does sizeof(*“327”) return 1 instead of 8 on a 64 bit system?

假装没事ソ 提交于 2019-12-09 07:46:38
问题 printf("%lu \n", sizeof(*"327")); I always thought that size of a pointer was 8 bytes on a 64 bit system but this call keeps returning 1. Can someone provide an explanation? 回答1: Putting * before a string literal will dereference the literal (as string literal are array of characters and will decay to pointer to its first element in this context). The statement printf("%zu \n", sizeof(*"327")); is equivalent to printf("%zu \n", sizeof("327"[0])); "327"[0] will give the first element of the

Number of bits in a data type

北城以北 提交于 2019-12-09 06:45:47
问题 I have two tasks for an assignment, one return the number of bits in type int on any machine. I thought I would write my function like so: int CountIntBitsF() { int x = sizeof(int) / 8; return x; } Does that look right? The second part is to return the number of any bits of any data type with a macro, and the macro can be taken from limits.h. I looked up limits.h on my machine, and also http://www.opengroup.org/onlinepubs/007908799/xsh/limits.h.html, but I don't think I really understand how

Why function does not know the array size?

拥有回忆 提交于 2019-12-09 03:46:16
问题 If I write int main() { int a[100] = {1,2,3,4,}; cout<<sizeof(a)/sizeof(a[0])<<endl; //a is a pointer to the first elem of array, //isn't it return 0; } I get 400! If I write void func(int *a); int main() { int a[100] = {1,2,3,4,}; func(a); return 0; } void func(int *a) { cout<<sizeof(a)/sizeof(a[0])<<endl; //a is a pointer to the first elem of array } Then I get 1! So why function does not know the array size? 回答1: Arrays decay to pointers when passed to functions, so all you will get is the

Does Unary + operator do type conversions?

爱⌒轻易说出口 提交于 2019-12-09 00:30:32
问题 Till now I was believing that there is no use of unary + operator. But then I came across with following example: char ch; short sh; int i; printf("%d %d %d",sizeof(ch),sizeof(sh),sizeof(i)); // output: 1 2 4 printf("%d %d %d",sizeof(+ch),sizeof(+sh),sizeof(i)); // output: 4 4 4 Does it mean + is doing type conversion here? Because it is behaving same as following printf("%d %d %d",sizeof((int)ch),sizeof((int)sh),sizeof(i)); // output: 4 4 4 This forces me to think + is doing type conversion.

Size of a struct with flexible array member

Deadly 提交于 2019-12-08 20:19:45
问题 Given struct Foo { uint32_t a; uint32_t b[]; }; What is sizeof(Foo) ? Is it implementation-defined or undefined behaviour? Does the answer differ for C vs C++? 回答1: The compiler will ignore the flexible array member as it were not there. C11-§6.7.2.1 (p18) [...] In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply [...].

What is sizeof(2.5) equal to? [closed]

守給你的承諾、 提交于 2019-12-08 17:47:29
Closed . This question is opinion-based . It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post . Closed 3 years ago . An examination question was: Q2: sizeof(2.5) is equal to ____. A.1 B.2 C.3 D.4 There are no such suffixes as f l F L , so floating-point constant (2.5) has a double type. Since nothing else is specified, I suppose its value is unspecified. For example, on some implementations whose CHAR_BIT is 32(causing sizeof (char)==sizeof (long) , maybe), the answer can be B. However, in

how to get byte size of type in generic list?

人盡茶涼 提交于 2019-12-08 16:19:56
问题 I have this generic list and I want to get the byte size of the type like if T is string or int etc., I tried both ways as written in getByteSize(), and just to let you know I am using only one way at a time ... but when I try to compile, it gives an error saying "Error: The type or namespace name 'typeParameterType' could not be found (are you missing a using directive or an assembly reference?)" public class iList<T> : List<T> { public int getByteSize () { // way 1 Type typeParameterType =

Windows下C语言实现 hello/hi 多线程网络聊天程序以及代码分析

早过忘川 提交于 2019-12-08 13:19:32
一、编译环境   系统:Windows 10  软件:CodeBlocks 17.12 二、完整代码 server: 1 #include <stdio.h> 2 #include <windows.h> 3 #include <Winsock2.h> 4 #include <string.h> 5 #include <pthread.h> 6 7 #pragma comment("ws2_32.lib") 8 9 struct mes{ 10 SOCKET clisock; 11 SOCKADDR_IN cliaddr; 12 }; 13 14 void* thread_new(void *); 15 16 int main() 17 { 18 WORD wVersionRequested; 19 WSADATA wsaData; 20 wVersionRequested = MAKEWORD(2,2); 21 if(WSAStartup(wVersionRequested, &wsaData) != 0) 22 { 23 printf("WSAStarup Failed!\n");//初始化错误 24 exit(-1); 25 } 26 if(wsaData.wVersion != wVersionRequested) 27 { 28 printf("The version

Pointers - Casting as Struct Pointer + Malloc

我的未来我决定 提交于 2019-12-08 09:23:28
问题 I have some ideas of what this would do, but I'd like a more professional and experienced explaination. typedef struct{ char str[50]; unsigned short num; }s; s *name = (s *) malloc(sizeof(s)); The first part defines a struct. I have no problem with that This will create a pointer to "s" struct. malloc() returns a memory address that will be cast as a pointer to "s". However, sizeof(s) I Believe I am having some issues understanding. char str[50]; = 50 bytes. unsigned short num; = 2 bytes.

Why the sizeof character constant is 4 bytes? [duplicate]

岁酱吖の 提交于 2019-12-08 08:12:57
问题 This question already has answers here : Size of character ('a') in C/C++ (4 answers) Closed 2 years ago . The program below produces output - 1 4 4 #include<stdio.h> void main() { char ch; ch='A'; printf("%d %d %d\n",sizeof(ch),sizeof('A'),sizeof(3.2f)); } Why is the size of character constant is 4 bytes ? 回答1: Because according to the C standard the type of a character constant is int , and not char . So in effect, this is the sizeof(int) on your platform. 回答2: ch is char type so 1 byte. 'A