char*a=malloc(sizeof*1024); sizeof(a) a为什么是4. Let's break it down right to left.

The product of these parameters determines the size of the memory block is allocated. もちろん間違いではありませんが、この場合の「sizeof(char)」は、単純に「1」に置き換えても大丈夫です。 (∵sizeof(char)は1である事が約束されているから) しかし、VC++特有のTCHAR型配列のメモリを確保する場合等は、間違いなくsizeof(TCHAR)にするべきですね☆ ansi c规定,sizeof(char) = 1。因此,符合标准的C编译器,两者结果一样。但是也不排除有那么一些另类的编译器有什么不同。
RT! char *a = (char*)malloc(sizeof(char) * 1024); printf("%d\n", sizeof(a)); a为什么会是4啊? char如果按1B算 结果应该是1024B才对啊。 如果申请失败了,a[1000]='c'; 这句话却是成功的。 在下系统win10 64。 蒙了..... \t\tsizeof(char*)几个字节? In order to understand what that (int *) is doing there, it's helpful to understand what malloc(sizeof(int)) is doing.

A more intelligent solution would be to malloc() the "C string" in your function, strncpy() the contents of y, and return the pointer to that memory area. This also means that your two calls to sizeof will always return 4 (size of a pointer).

char *方の配列を動的確保する必要が出たのですが、char **array=(char **)malloc((char *) * 10);としてうまくいきませんどうすれば確保できるのでしょうか知っている方がいましたら教えてくださいちなみに確保した配列はこの様に使える

The only indication that it has failed is if malloc returns NULL; if it does, it would probably make most sense to immediately return that NULL pointer.

char* reverse_string(const char* string) Check for NULL pointers. Better add another parameter with the size of the array.

The code must avoid dereferencing a NULL pointer if the call to malloc fails. davidb schrieb: Hi, does someone know how to get the length of a 2 dimensional string array: It is a one dimensional C-style-string array. Or a 2 dimensional char array. When you call the calloc() function, you need to pass 2 parameters: the first one specify the number of elements and the second one specify the size of each element. char *p只是定 2113 义了一个指针 类型 的 5261 变量,并没有 给该 指针分配空间,进 行初 始 4102 化 1653 ,不能 通过 该指针进行返问。 char *p=(char*)malloc(sizeof(char)),定义了一个指针类型的变量p,并给该指针动态分配了一个字符的空间,可以通过该指针进行访问该空间。 是分 配10个大小为char,也就是 2113 十个字 5261 节 的 内存 给 str,地址类型是char指针 str = (char*)calloc(10,sizeof(char));的 (char*)和(char)可以 4102 换成其他的 比如 str = (Int*)calloc(10,sizeof(int)); 这样写就是分配10x4=40个字节 1653 的地址给str sizeof is a unitary operator in the programming languages C and C++.It generates the storage size of an expression or a data type, measured in the number of char-sized units.Consequently, the construct sizeof (char) is guaranteed to be 1.The actual number of bits of type char is specified by the preprocessor macro CHAR_BIT, defined in the standard include file limits.h. Learn to use pointers instead of indexing The calloc() function allocates memory much like malloc() function except the following differences:.