sizeof

C++之指针与数组区别

点点圈 提交于 2019-11-27 10:05:32
C++/C程序中,数组要么在静态存储区被创建(如全局数组),要么在栈上被创建。数组名对应着(而不是指向)一块内存,其地址与容量在生命期内保持不变,只有数组的内容可以改变。指针可以随时指向任意类型的内存块,它的特征是“可变”,所以我们常用指针来操作动态内存。指针远比数组灵活,但也更危险。 下面以字符串为例比较指针与数组的特性。 一、修改内容 1 char a[] = “hello”; //“hello”位于常量存储区,a在全局(静态)初始化区 2 a[0] = ‘X’; 3 cout << a << endl; 4 char *p = “world”; // 注意p指向常量字符串 p位于全局(静态)初始化区 5 p[0] = ‘X’; // 编译器不能发现该错误 常量字符串的内容是不能被修改的 6 cout << p << endl; 二、内容复制与比较 1 // 数组… 2 char a[] = "hello"; //a在全局(静态)初始化存储区 “hello”在常量存储区 3 char b[10]; //b在全局(静态)未初始化存储区 4 strcpy(b, a); // 不能用 b = a; 5 if(strcmp(b, a) == 0) // 不能用 if (b == a) 6 … 7 // 指针… 8 int len = strlen(a); 9 char *p =

Why is (sizeof(int) > -1) false? [duplicate]

好久不见. 提交于 2019-11-27 09:51:36
This question already has an answer here: Why should be there the involvement of type promotion in this code? 1 answer Comparison operation on unsigned and signed integers 7 answers Can You justify the below code: #include<stdio.h> int main() { if(sizeof(int) > -1) { printf("\nTrue\n"); } else { printf("\nFALSE\n"); } } The output is FALSE .....suggest me the reason sizeof(int) has type size_t , which is an unsigned integer type. -1 has type int , which is a signed integer type. When comparing a signed integer with an unsigned integer, first the signed integer is converted to unsigned, then

Why sizeof(int) is not greater than -1? [duplicate]

元气小坏坏 提交于 2019-11-27 09:47:48
This question already has an answer here: Comparison operation on unsigned and signed integers 7 answers this is my C code : why is the output "False " ????? why 4 > -1??? code : #include <stdio.h> int main() { if (sizeof(int) > -1) printf("True"); else printf("False"); return 0; } Because sizeof(int) is unsigned. So -1 is converted to a large unsigned value. Because sizeof yields a value of type size_t which is an unsigned type. In > expression usual arithmetic conversions will convert -1 to an unsigned type which is the type of the > result. The resulting value will be a huge positive value.

What decides the sizeof an integer?

余生颓废 提交于 2019-11-27 09:44:43
sizeof(int) shows 4 on my Dev Cpp even though its running on a 64 bit machine. Why doesn't it consider the underlying HW and show 8 instead? Also, if I compiling environment also changes to 64 bit ( Does a 64 bit compiler makes sense in the first place?! ), would size of int change then? Are there any standards which decide this? Taken from http://en.wikipedia.org/wiki/64-bit (under 64-bit data models ) There are various models, Microsoft decided that sizeof(int) == 4 , some (a few) others didn't. HAL Computer Systems port of Solaris to SPARC64 and Unicos seem to be the only ones where sizeof

c语言面试题__指针篇

心不动则不痛 提交于 2019-11-27 09:41:52
文章转载自: http://www.pythonheidong.com/blog/article/2541/ 1. char * const p;   char const * p   const char *p   上述三个有什么区别?   char * const p; //常量指针,p的值不可以修改   char const * p;//指向常量的指针,指向的常量值不可以改   const char *p; //和char const *p ------------------------------------------------------ 2. char str1[] = "abc";   char str2[] = "abc";   const char str3[] = "abc";   const char str4[] = "abc";   const char *str5 = "abc";   const char *str6 = "abc";   char *str7 = "abc";   char *str8 = "abc";   cout << ( str1 == str2 ) << endl;   cout << ( str3 == str4 ) << endl;   cout << ( str5 == str6 ) << endl;   cout

C -> sizeof string is always 8

南楼画角 提交于 2019-11-27 09:23:40
#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); } Why does it always return 8??? There is no string data type in C. Is this C++? Or is string a typedef? Assuming

Getting the size of a malloc only with the returned pointer

折月煮酒 提交于 2019-11-27 09:20:55
I want to be able to vary the size of my array so I create one this way: int* array; array = malloc(sizeof(int)*10);//10 integer elements I can use this like an array as you normally would, however when I try to find the size of it like so: size = sizeof(array)/sizeof(int); I get the answer 1 because its not recognizing it as pointing to an array How can I get the size of the array ? (I know its not technically an array but is there a way to work out the whole size of the allocated memory block ?) Also am I right in assuming what I have stated in the description ? If I am technically wrong

C pointers and arrays/ 'sizeof' operator [duplicate]

寵の児 提交于 2019-11-27 09:15:22
Possible Duplicate: Stack pointer difference for char pointer and array To illustrate my question: int main(void){ int myary[20]; int *myaryPtr; myaryPtr = myary; sizeof(myary); // Will it return 80? Correct? sizeof(myaryPtr); // Will it return 4? Correct? return 0; } First off, is my assumption correct? And then assuming my assumption is correct, what is the detailed explanation? I understand that my 20 element array is 80 bytes, but isn't the name myary merely a pointer to the first element of the array? So shouldn't it also be 4? Yes, your assumption is correct, assuming an int and a

3-byte int and 5-byte long?

守給你的承諾、 提交于 2019-11-27 09:04:37
Does each of C and C++ standards allow sizeof of numeric types not to be a power of two? The following constraints are known: 16 <= CHAR_BIT * sizeof(int) <= CHAR_BIT * sizeof(long) 32 <= CHAR_BIT * sizeof(long) <= CHAR_BIT * sizeof(long long) and a dozen of others, which on a typical 8-bit byte architecture means 2 <= sizeof(int) && 4 <= sizeof(long) Does that mean that sizeof(int) == 3 && sizeof(long) == 5 is a valid behaviour? If yes - is there any known compiler/architecture behaving in a similar way? I think 3.9.1/2 (C++98) sums this up nicely (immediately followed by analogous

why sizeof('a') is 4 in C? [duplicate]

大城市里の小女人 提交于 2019-11-27 08:56:17
Possible Duplicate: Why are C character literals ints instead of chars? #include<stdio.h> int main(void) { char b = 'c'; printf("here size is %zu\n",sizeof('a')); printf("here size is %zu",sizeof(b)); } here output is (See live demo here .) here size is 4 here size is 1 I am not getting why sizeof('a') is 4 ? Because in C character constants, such as 'a' have the type int . There's a C FAQ about this suject: Perhaps surprisingly, character constants in C are of type int , so sizeof('a') is sizeof(int) (though this is another area where C++ differs). The following is the famous line from the