How to cast an integer to void pointer?

While working with Threads in C, I'm facing the warning "warning: cast to pointer from integer of different size" The code is as follows #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<pthread.h> void *print(void *id) { int a=10; printf("My thread id is %ldn",pthread_self()); printf("Thread %d is executingn",id); return (void *) 4

如何将整型转换为void指针?

在C中使用线程时,我正面临警告 “警告:从不同大小的整数转换为指针” 代码如下 #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<pthread.h> void *print(void *id) { int a=10; printf("My thread id is %ldn",pthread_self()); printf("Thread %d is executingn",id); return (void *) 42; } int main() { pthread_t th[5]; int t; int i; int status; void *ret;

Why / when to use `intptr

I have a question regarding using intptr_t vs. long int . I've observed that incrementing memory addresses (eg via manual pointer arithmetic) differs by data type. For instance incrementing a char pointer adds 1 to the memory address, whereas incrementing an int pointer adds 4, 8 for a double, 16 for a long double, etc... At first I did something like this: char myChar, *pChar; float myF

为什么/何时使用`intptr

我有一个关于使用intptr_t与long int 。 我发现递增内存地址(例如通过手动指针算术)因数据类型而异。 举个例子,增加一个字符指针会在内存地址中加1,而增加一个int指针则会增加4,8为一个双精度值,16为一个长精度等等。 起初我做了这样的事情: char myChar, *pChar; float myFloat, *pFloat; pChar = &myChar; pFloat = &myFloat; printf( "pChar: %dn", ( int )pChar ); printf( "pFloat: %dn", ( int )pFlo

t instead of void*?

Is it a good idea to use intptr_t as a general-purpose storage (to hold pointers and integer values) instead of void* ? (As seen here: http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html) For what I've already read: int -> void* -> int roundtrip is not guaranteed to hold original value; I guess int -> intptr_t -> int will do poin

而不是void *?

使用intptr_t作为通用存储(保存指针和整数值)而不是void*是个好主意吗? (如此处所示:http://www.crystalspace3d.org/docs/online/manual/Api1_005f0-64_002dBit-Portability-Changes.html) 对于我已经读过的内容: int - > void* - > int往返不保证保持原始值; 我猜int - > intptr_t - > int会做 void*和intptr_t上的指针算术都需要强制转换,所以这里没有任何优势 void*表示存储指针时的较少显式转

return of malloc() function

malloc() function is said to return a null pointer or a pointer to the allocated space. Suppose for a string we make the statement: char* ptr = malloc(size) Isn't ptr a pointer that would point to a pointer? Isn't : char** ptr = malloc(size) supposed to be the correct way to declare the pointer to char? The compiler however doesn't give a warning when we do either, the w

malloc()函数的返回

malloc()函数据说返回一个空指针或一个指向已分配空间的指针。 假设对于一个字符串,我们做出如下陈述: char* ptr = malloc(size) ptr不是指向指针的指针吗? 不是: char** ptr = malloc(size) 应该是正确的方式来声明指向char的指针? 但是,编译器在我们执行任何操作时都不会给出警告,但它提供的警告是使用格式说明符。 我们应该如何申报?为什么? 另外,如何使用char **? char *表示一个字符指针。

Type casting in malloc

This question already has an answer here: Do I cast the result of malloc? 27 answers If you haven't #include d stdlib.h , the return value of malloc is most likely truncated before it gets returned to the calling code. In theory, it is undefined behavior. If the return value of malloc is truncated, it's analogous to using: int a = 10; int* ap = &a; int temp = (int)ap; // Th

在malloc中输入类型

这个问题在这里已经有了答案: 我输入malloc的结果吗? 27个答案 如果您没有#include d stdlib.h , malloc的返回值在返回到调用代码之前很可能被截断。 从理论上讲,这是未定义的行为。 如果malloc的返回值被截断,则类似于使用: int a = 10; int* ap = &a; int temp = (int)ap; // This is where you lose the pointer value due to truncation. int* bp = (int*)temp; 你的房子宽50英尺。 你的家庭地址是&#

Casting errors in malloc for "char" array in C language

Code to read bmp image using struct variables and struct array. Kindly suggest me correct way to do typecasting to malloc(errors listed below code): #include<stdio.h> #include<stdlib.h> typedef struct bands{ /* .bmp image store pixel colors in "bgr" sequence */ unsigned char b,g,r; //in 24bit bmp, we need to use 8bit datatype for each color }bands; int main() { FILE *bmpimage; //p

在malloc中为C语言中的“char”数组铸造错误

使用结构变量和结构数组读取bmp图像的代码。 请建议我以正确的方式对malloc进行类型转换(错误代码如下): #include<stdio.h> #include<stdlib.h> typedef struct bands{ /* .bmp image store pixel colors in "bgr" sequence */ unsigned char b,g,r; //in 24bit bmp, we need to use 8bit datatype for each color }bands; int main() { FILE *bmpimage; //ptr to read image file FILE *redpix,*greenpix,*bl

Malloc confusion

Hi after going through answer here 1: Do I cast the result of malloc? I understood that one of the reason why we do not cast malloc is that casting malloc is redundant But what I am still trying to figure out is the warning that will be suppressed when we do cast the malloc function I also read this answer but I have the follwing doubts #include<stdio.h> main() { int *a=malloc

Malloc困惑

嗨,经过这里回答 1:我输入malloc的结果吗? 我明白我们之所以不投malloc的原因之一就在于此 铸造malloc是多余的 但是我仍然试图弄清楚的是当我们施放malloc函数时会被抑制的警告 我也读过这个答案,但我有以下疑惑 #include<stdio.h> main() { int *a=malloc(20); } 我理解了答案中的一点,即编译器会认为malloc返回一个int,而我们试图将该值赋给一个int *,这会给我们带来错误,无法从int *转换为int或

how to pass strings to char array in C

char array[2]; array = "h"; Gives the following error: error: incompatible types when assigning to type 'char[2]' from type 'char' char array[2]; array[] = "h"; Gives the following warning: warning: assignment makes integer from pointer without a cast I tried these two ways to pass the value h into the array but I can't. I want to know what the problem is. Arrays ca

如何将字符串传递给C中的char数组

char array[2]; array = "h"; 给出以下错误: 错误:从类型'char'分配类型'char [2]'时不兼容的类型 char array[2]; array[] = "h"; 给出以下警告: 警告:赋值使得指针中的整数不带强制转换 我尝试了这两种方法将值h传递给数组,但我不能。 我想知道问题是什么。 数组不能指向新的地址。 你可以用一个字符串常量来初始化它们: char array[2] = "h"; 或者您可以稍后将数据复制到阵列中: char a

is it necessary to type

Possible Duplicate: Do I cast the result of malloc? I was googling to find out the reason for type-casting of malloc and calloc . But, i only found type-casting of malloc is not necessary since it return void pointer but, what about calloc . This is the same reason for calloc too ??? Now, if we move back to first point, about return value of malloc and calloc . Then, i found that, both a

是否需要输入

可能重复: 我输入malloc的结果吗? 我在Google上寻找malloc和calloc类型转换的原因。 但是,我只发现malloc的类型转换没有必要,因为它返回void指针,但calloc呢 。 这也是calloc的原因? 现在,如果我们回到第一点,关于malloc和calloc的返回值。 然后,我发现,两者都返回分配的空间。 所以,我在这里有点困惑。 所以,我的问题是 malloc和calloc的返回值是什么 是否需要输入malloc和calloc 。 为什么? ma

free a casted pointer

I want to know if it is ok to free() a pointer casted to another type. For instance if I do this: char *p = malloc (sizeof (int)); int *q = (int *)p; free (q); I get no warning on gcc (-Wall). On linux, the man pages on free says it is illegal to call free on a pointer that was not returned by malloc(), calloc() or realloc(). But what happens if the pointer was cast to another type in betwe

释放一个铸造的指针

我想知道是否可以释放()一个指向另一个类型的指针。 例如,如果我这样做: char *p = malloc (sizeof (int)); int *q = (int *)p; free (q); 我在gcc(-Wall)上没有任何警告。 在Linux上,free页面上的man页面表示,通过malloc(),calloc()或realloc()不返回的指针调用free是非法的。 但是如果指针被转换为另一种类型,会发生什么? 我问这是因为我读了C标准不需要不同的指针类型(例如int *和char *)具有相同