sizeof

Getting wrong string length

自古美人都是妖i 提交于 2019-12-20 06:19:03
问题 I am trying to get the length of a string but i am getting the wrong value, it is saying that it is only 4 characters long. Why is this? am i using sizeof() correctly? #include <stdio.h> int main(void) { char *s; int len; s = "hello world"; len = sizeof(s); printf("%d\n", len); } 回答1: The sizeof operator is returning the size of the pointer. If you want the length of a string, use the strlen function. Even if you had an array (e.g. char s[] = "hello world" ) the sizeof operator would return

How to know the size (in bytes) of a char array passed to a function?

女生的网名这么多〃 提交于 2019-12-20 05:36:48
问题 I am testing the sizeof operator. In two cases in my code, I get the size of the pointer (I think). In the other cases I get how many bytes the arrays occupy . How can I get the size of the array in bytes when I pass it to a function? Isn't the sizeof operator enough? Am I doing something wrong? #include <stdio.h> /*Testing the operator sizeof*/ void test (char arrayT[]); void test2 (char *arrayU); int main(int argc, char *argv[]){ char array1[7]; printf("array1 size is:%d\n", sizeof array1);

Sizes of arrays declared with pointers

纵然是瞬间 提交于 2019-12-20 04:53:56
问题 char c[] = "Hello"; char *p = "Hello"; printf("%i", sizeof(c)); \\Prints 6 printf("%i", sizeof(p)); \\Prints 4 My question is: Why do these print different results? Doesn't c[] also declare a pointer that points to the first character of the array (and therefore should have size 4, since it's a pointer)? 回答1: It sounds like you're confused between pointers and arrays. Pointers and arrays (in this case char * and char [] ) are not the same thing. An array char a[SIZE] says that the value at

Why the array size is 1 [duplicate]

限于喜欢 提交于 2019-12-20 04:09:11
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Sizeof an array in the C programming language? I'm trying to write a function that return 1s if a value is in the array. Here's the code: int inArrayInt(int iVal, int iArray[]) { int i; int arrayL = sizeof(*iArray) / sizeof(int); int flag = 0; for(i=0; i < arrayL; i++) { if(iVal == iArray[i]) { flag = 1; } } return flag; } The problem is that arrayL = sizeof(*iArray) / sizeof(int); always evaluates to 1, even if

What is the easiest way to find the sizeof a type without compiling and executing code?

我们两清 提交于 2019-12-20 03:32:10
问题 I wrote a bash script to determine the size of gcc 's datatypes (e.g. ./sizeof int double outputs the respective sizes of int and double ) by wrapping each of its arguments in the following P() macro and then compiling and running the code. #define P(x) printf("sizeof(" #x ") = %u\n", (unsigned int)sizeof(x)) The problem is that this is relative slow (it takes a whole second!), especially the linking step (since compiling with -c or -S takes virtually no time, and so does running the

线性基总结

半腔热情 提交于 2019-12-20 02:32:05
线性基    线性基是向量空间的一组基,通常可以解决有关异或的一些题目。通俗一点的讲法就是由一个集合构造出来的另一个集合,它有以下几个性质: 线性基的元素能相互异或得到原集合的元素的所有相互异或得到的值。 线性基是满足性质 1 的最小的集合。 线性基没有异或和为 0 的子集。 线性基中每个元素的异或方案唯一,也就是说,线性基中不同的异或组合异或出的数都是不一样的。 线性基中每个元素的二进制最高位互不相同。 线性基中每个元素的二进制最高位互不相同。    构造线性基的方法如下   对原集合的每个数 \(x\) 转为二进制,从高位向低位扫,对于第 \(i\) 位是 \(1\) 的,如果 \(p_i\) 不存在,那么令 \(p_i = x\) 并结束扫描,如果存在,令 \(x = x\) \(\oplus\) \(p_i\) 。 inline void insert(long long x) { for (int i = 55; i + 1; i--) { if (!(x >> i)) // x的第i位是0 continue; if (!p[i]) { p[i] = x; break; } x ^= p[i]; } } 查询原集合内任意几个元素 \(xor\) 的最大值,就可以用线性基解决。 将线性基从高位向低位扫,若 $xor \(上当前扫到的\) a_x$答案变大,就把答案异或上

图解c/c++多级指针与“多维”数组

北城余情 提交于 2019-12-19 23:22:28
声明: 本文为原创博文,如有转载,请注明出处。若本文有编辑错误、概念错误或者逻辑错误,请予以指正,谢谢。 指针与数组是C/C++编程中非常重要的元素,同时也是较难以理解的。其中,多级指针与“多维”数组更是让很多人云里雾里,其实,只要掌握一定的方法,理解多级指针和“多维”数组完全可以像理解一级指针和一维数组那样简单。 首先,先声明一些常识,如果你对这些常识还不理解,请先去弥补一下基础知识: 1、实际上并不存在多维数组,所谓的多维数组本质上是用一维数组模拟的。 2、数组名是一个常量(意味着不允许对其进行赋值操作),其代表数组首元素的首地址。 3、数组与指针的关系是因为数组下标操作符[],比如,int a[3][2]相当于*(*(a+3)+2) 。 4、指针是一种变量,也具有类型,其占用内存空间大小和系统有关,一般32位系统下,sizeof(指针变量)=4。 5、指针可以进行加减算术运算,加减的基本单位是sizeof(指针所指向的数据类型)。 6、对数组的数组名进行取地址(&)操作,其类型为整个数组类型。 7、对数组的数组名进行sizeof运算符操作,其值为整个数组的大小(以字节为单位)。 8、数组作为函数形参时会退化为指针。 一、一维数组与数组指针 假如有一维数组如下: char a[3]; 该数组一共有3个元素,元素的类型为char,如果想定义一个指针指向该数组

Behaviour of sizeof with string

送分小仙女□ 提交于 2019-12-19 21:42:29
问题 ‪#‎include‬ <stdio.h> #include <string.h> int main() { printf("%d\n",sizeof("S\065AB")); printf("%d\n",sizeof("S65AB")); printf("%d\n",sizeof("S\065\0AB")); printf("%d\n",sizeof("S\06\05\0AB")); printf("%d\n",sizeof("S6\05AB")); printf("%d\n",sizeof("\0S65AB")); return 0; } output: 5 6 6 7 6 7 http://ideone.com/kw23IV Can anyone explain this behaviour with character strings? Using GCC on Debian 7.4 回答1: The size of a string literal is the number of characters in it including the trailing null

C memset seems to not write to every member

浪尽此生 提交于 2019-12-19 18:57:14
问题 I wrote a small coordinate class to handle both int and float coordinates. template <class T> class vector2 { public: vector2() { memset(this, 0, sizeof(this)); } T x; T y; }; Then in main() I do: vector2<int> v; But according to my MSVC debugger, only the x value is set to 0, the y value is untouched. Ive never used sizeof() in a template class before, could that be whats causing the trouble? 回答1: No don't use memset -- it zeroes out the size of a pointer (4 bytes on my x86 Intel machine)

Operator 'sizeof' with conditional (ternary) expression

我与影子孤独终老i 提交于 2019-12-19 12:53:10
问题 I have a hard time understanding sizeof 's behaviour when given a ternary expression. #define STRING "a string" int main(int argc, char** argv) { int a = sizeof(argc > 1 ? STRING : ""); int b = sizeof(STRING); int c = sizeof(""); printf("%d\n" "%d\n" "%d\n", a, b, c); return 0; } In this example (tested with gcc 4.4.3 and 4.7.2, compiled with -std=c99 ), b is 9 (8 characters + implicit '\0' ), c is 1 (implicit '\0' ). a, for some reason, is 4 . I would expect a to be either 9 or 1, based on