c - Casting the void pointer and printing with format specifiers -


okay school project question has nothing regarding specific implementation, nor asking regarding project itself. giving warning because want give context doing, rather general question regarding casting of void pointers, framed within context of project... function i'm writing isn't project, it's wrote testing mechanism see if generation of data structure working... , lead me problem involving printf(), format specifiers, , casting.... have implement linked-list (called 'queue' not queue @ all), , wrote function test it.

there struct called "chunk" variables (first index within larger array first element of given "chunk", , arr larger_array[c->first], it's void pointer such: void *arr c pointer chunk) indicate position in larger array. if have array = {5,7,3,4,6,9,1,2}, , given chunk size of two, have 4 chunks each of whom have "arr" void pointer variable point respectively 5, 3, 6, 1.

anyways, wrote "print_queue" function (actually linked-list of chunks) , yay! prints information desired, , here core part share:

 while (index < num_chunks) {     first = c->first;     printf("chunk %d: first %d , a[%d] ", index, first, first);     if (elem_size == long_size) /* long_size defined above 8 */       printf("%ld\n", c->arr);     else if (elem_size == int_size) /* int_size defined above 4 */       printf("%d\n", c->arr);     else if (elem_size == char_size) /* char_size defined above 1 */       printf("%c\n", c->arr);      index++;      if (c->next != null)       c = c->next;  } 

i wanted write function able print linked-list of 3 types (longs, ints, , chars) testing purposes while implement actual functionality of project (multithreaded merge-sort). code above works! output array input:

 char original[] = {'z', 'y', 'x', 'w', 'v', 'u', 't', 's'}; 

output:

 chunk 0: first 0 , a[0] z  chunk 1: first 2 , a[2] x  chunk 2: first 4 , a[4] v   chunk 3: first 6 , a[6] t 

so works! yay! however, these compiler warnings:

mergesort.c: in function 'print_chunk_queue': mergesort.c:85:7: warning: format '%ld' expects argument of type 'long int',     argument 2 has type 'void *' [-wformat] mergesort.c:87:7: warning: format '%d' expects argument of type 'int', argument 2 has type 'void *' [-wformat] mergesort.c:89:7: warning: format '%c' expects argument of type 'int', argument 2 has type 'void *' [-wformat] 

so did cast c->arr 's (type *) c->arr, gave me more warnings "oh want int have int pointer" did then:

 * ((type *) c->arr) 

basically dereferencing casted void pointer (which never null! pointing valid numbers, @ least input provided it!), , gives me segmentation fault!. i'm pretty frustrated because went working output ton of "warnings" useless segmentation faults.

edit:

definition of data structure, requested:

typedef struct chunk {   void *arr;   struct chunk *next;   int first; } chunk; 

this how setting state of single chunk , creating linked-list of chunks:

  while (index < number_of_chunks) {     if (index == 0) {       if ((current = malloc(sizeof(chunk))) == null)         err(ex_oserr, "memory allocation has failed\n");       head = current;     }     current->first = (chunk_size * index);     current->arr = ((char *) array)[current->first];     current->size = chunk_size;     if (index != (number_of_chunks - 1)) {       if ((current->next = malloc(sizeof(chunk))) == null)         err(ex_oserr, "memory allocation has failed\n");       current = current->next;    }   else {     tail = current;   }   index += 1; } 

current->arr = ((char *) array)[current->first]; 

there should & here. want assign address of byte arr rather value.

current->arr = &((char *) array)[current->first]; 

if arr contain address should, allow casts & dereferences work.

printf("%ld\n", *(long *) c->arr); printf("%d\n",  *(int  *) c->arr); printf("%c\n",  *(char *) c->arr); 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -