LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2023, 02:38 PM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869

Your program is not unsalvable; please make an effort to understand the changes.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void do_qsort(void **ar, int(*cmp)(const void *, const void *), int left, int right);

static int cmp(const void *pa, const void *pb)
{
    int a= *(int *)pa;
    int b= *(int *)pb;
    if (a<b)       return -1;
    else if (a==b) return  0;
    else           return  1;
}

int main(){
    int *intvals = NULL; //array of integers
    int **vals= NULL;//array of pointers
    int total_elements;

    if (isatty(fileno(stdin))) {
        printf("How many elements in the array");
    }
    scanf("%d", &total_elements);

    intvals = malloc(total_elements*sizeof(int));
    vals = malloc(total_elements*sizeof(int *));
    for(int i= 0; i<total_elements; ++i) {
        vals[i]= &intvals[i];
        scanf("%d", vals[i]);
    }
    printf("unsorted:");
    for (int i=0; i<total_elements; ++i) {
        printf(" %d", *vals[i]);
    }
    fputc('\n', stdout);

    do_qsort((void **)vals, cmp, 0, total_elements-1);

    printf("sorted:");
    for (int i=0; i<total_elements; ++i) {
        printf(" %d", *vals[i]);
    }
    fputc('\n', stdout);

    free(vals);
    free(intvals);
    return 0;
}

int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int pivotIndex);

static void swap(void **arr, int i, int j) {
    void *tmp= arr[i];
    arr[i]= arr[j];
    arr[j]= tmp;
}

void do_qsort(void **ar, int(*cmp)(const void *, const void *), int left, int right){
    int pivotIndex;

    if (right<= left) return;
    else if (left+1==right) {
        if (cmp(ar[left], ar[right])>0) {
            swap(ar, left, right);
        }
        return;
    }

    pivotIndex = (left+right+1)/2;
    pivotIndex = partition(ar, cmp, left, right, pivotIndex);
    do_qsort(ar, cmp, left, pivotIndex-1);
    do_qsort(ar, cmp, pivotIndex+1, right);
}

int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int pivotIndex){
       int idx, store; void *pivot = ar[pivotIndex]; void *tmp = ar[right];
       ar[right] = ar[pivotIndex];  ar[pivotIndex]= tmp;
       store = left;
       for(idx= left; idx<right;idx++){
           if(cmp(ar[idx], pivot) <=0){
                  tmp = ar[idx]; ar[idx]=ar[store]; ar[store]=tmp; store++;
           }
       }
       tmp= ar[right];  ar[right] = ar[store];  ar[store] = tmp;

       return store;
}

Last edited by NevemTeve; 06-22-2023 at 02:45 PM.
 
1 members found this post helpful.
Old 06-28-2023, 05:03 AM   #17
digitechroshni
LQ Newbie
 
Registered: Jun 2023
Location: Bhubaneswar
Posts: 2

Rep: Reputation: 1
There is an error in the code you provided. The error is in the line where you use the "scanf" function inside the first for loop:
scanf("%d", ((int *)arr)[i]);

The issue is with the argument you're passing to scanf. The scanf function expects the address of the variable where it should store the input value, using the & operator. However, in your code, you're not providing the address correctly.

To fix the error, you need to pass the address of the array element to scanf. You can do this by using the & operator before ((int *)arr)[i]. The corrected line should be:
scanf("%d", &((int *)arr)[i]);

I hope this work!
Happy coding!
 
1 members found this post helpful.
Old 07-02-2023, 07:18 AM   #18
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by digitechroshni View Post
There is an error in the code you provided. The error is in the line where you use the "scanf" function inside the first for loop:
scanf("%d", ((int *)arr)[i]);

The issue is with the argument you're passing to scanf. The scanf function expects the address of the variable where it should store the input value, using the & operator. However, in your code, you're not providing the address correctly.

To fix the error, you need to pass the address of the array element to scanf. You can do this by using the & operator before ((int *)arr)[i]. The corrected line should be:
scanf("%d", &((int *)arr)[i]);

I hope this work!
Happy coding!
Thanks. Request to tell error in my earlier code, that fails by giving: 'Segmentation Fault', after accepting input numbers. It is also stated below.
My main concern is that, am unable to find the reason for segmentation fault; inspite of trying hard to figure the same.
Code:
#include <stdio.h>
#include<stdlib.h>
//int partition(void **, int, int *, int, int , int);
int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int pivotIndex);
int cmp(const void *, const void*);
//void do_qsort(void **, int *(const void *, const void*), int, int);
void do_qsort(void **ar, int(*cmp)(const void *, const void *), int left, int right);
int selectPivotIndex(int, int);

int main(){
	void ** vals;//array of pointers
        int total_elements, *intvals;
	printf("How many elements--in the array: : ");
	scanf("%d", &total_elements);
	vals = malloc(total_elements*sizeof(int));

        intvals = malloc(total_elements*sizeof(int));
        for(int i=0; i<total_elements; i++) scanf("%d", &intvals[i]);
        vals = malloc(total_elements*sizeof(int *));
 
        for(int i=0; i<total_elements; i++) *(int *)vals[i]= &intvals[i];   //*vals[i] = &intvals[i];
        do_qsort(vals, cmp, 0, total_elements-1);
        for(int i=0; i<total_elements; i++) printf("%d\n", *(int *)vals[i]);

	/*for(int i=0; i<total_elements; i++) scanf("%d", (int *)vals+i);
        //for(int **p=(int **)vals, **end = p+total_elements; **p<total_elements; p++)
	for(int **p=(int **)vals; **p<total_elements; p++)
		scanf("%d", *p);
	do_qsort(vals, cmp, 0, total_elements-1);
        printf("correct");
        for(int i =0; i<total_elements; i++)
        	printf("Element #%d, is: %d", i, vals[i]);
        return 1;*/
}

void do_qsort(void **ar, int(*cmp)(const void *, const void *), int left, int right){
	int pivotIndex;
        printf("q_sort");
        printf("entered do_qsort\n with left element index: %d, left element:%d, right element index: %d", left, (int *)(*ar+left), (int *)(*ar +right));// (int *)(*ar)[left], (int *)(*ar)[right]);
	if (right<= left) return;
        pivotIndex = selectPivotIndex(left, right);
	pivotIndex = partition(ar, cmp, left, right, pivotIndex);
	do_qsort(ar, cmp, left, pivotIndex-1);  do_qsort(ar, cmp, pivotIndex+1, right);
}

int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int pivotIndex){
       int idx, store; void *pivot = ar[pivotIndex]; void *tmp = ar[right]; 
       ar[right] = ar[pivotIndex];  ar[pivotIndex]= tmp;
       store = left;
       for(idx= left; idx<right;idx++){
	       if(cmp(ar[idx], pivot) <=0){
	              tmp = ar[idx]; ar[idx]=ar[store]; ar[store]=tmp; store++;
	       }
       }
       tmp= ar[right];  ar[right] = ar[store];  ar[store] = tmp;

       return store;
}

int cmp(const void * a, const void *b)
{
      if ((int*)a<=(int*)b)  return 0;
      else return 1;
}

int selectPivotIndex(int left, int right)
{
      int median;
      median = (left+right)/2;
      if(median %2==0) median = median +1;
      return median;
}

Segmentation fault:
Code:
$ ./quicksort
How many elements--in the array: : 5
10
5
23
8
29
Segmentation fault (core dumped)

Last edited by ajiten; 07-03-2023 at 12:17 AM.
 
Old 07-03-2023, 12:26 AM   #19
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,863

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
you might want to try valgrind. It can easily identify it.
(it is not that difficult, just a few lines of additional code: https://valgrind.org/docs/manual/qui...ck-start.mcrun)
 
2 members found this post helpful.
Old 07-03-2023, 01:28 AM   #20
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You forgot compiler-option -W -Wall -Werror -pedantic
Now try to debug this version:
Code:
#include <stdio.h>
#include<stdlib.h>
//int partition(void **, int, int *, int, int , int);
int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int pivotIndex);
int cmp(const void *, const void*);
//void do_qsort(void **, int *(const void *, const void*), int, int);
void do_qsort(void **ar, int(*cmp)(const void *, const void *), int left, int right);
int selectPivotIndex(int, int);

int main(){
    void ** vals;//array of pointers
        int total_elements, *intvals;
    printf("How many elements--in the array: : ");
    scanf("%d", &total_elements);
    vals = malloc(total_elements*sizeof(int*));

        intvals = malloc(total_elements*sizeof(int));
        for(int i=0; i<total_elements; i++) scanf("%d", &intvals[i]);
        vals = malloc(total_elements*sizeof(int *));

        for(int i=0; i<total_elements; i++) vals[i]= &intvals[i];   //*vals[i] = &intvals[i];
        do_qsort(vals, cmp, 0, total_elements-1);
        for(int i=0; i<total_elements; i++) printf("%d\n", *(int *)vals[i]);

    /*for(int i=0; i<total_elements; i++) scanf("%d", (int *)vals+i);
        //for(int **p=(int **)vals, **end = p+total_elements; **p<total_elements; p++)
    for(int **p=(int **)vals; **p<total_elements; p++)
        scanf("%d", *p);
    do_qsort(vals, cmp, 0, total_elements-1);
        printf("correct");
        for(int i =0; i<total_elements; i++)
        printf("Element #%d, is: %d", i, vals[i]);
        return 1;*/
}

void do_qsort(void **ar, int(*cmp)(const void *, const void *), int left, int right){
    int pivotIndex;
        printf("q_sort");
        printf("entered do_qsort\n with left element index: %d, left element:%d, right element index: %d",
             left, *(int *)ar[left], *(int *)ar[right]);
    if (right<= left) return;
        pivotIndex = selectPivotIndex(left, right);
    pivotIndex = partition(ar, cmp, left, right, pivotIndex);
    do_qsort(ar, cmp, left, pivotIndex-1);
    do_qsort(ar, cmp, pivotIndex+1, right);
}

int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int pivotIndex){
       int idx, store; void *pivot = ar[pivotIndex]; void *tmp = ar[right];
       ar[right] = ar[pivotIndex];  ar[pivotIndex]= tmp;
       store = left;
       for(idx= left; idx<right;idx++){
           if(cmp(ar[idx], pivot) <=0){
                  tmp = ar[idx]; ar[idx]=ar[store]; ar[store]=tmp; store++;
           }
       }
       tmp= ar[right];  ar[right] = ar[store];  ar[store] = tmp;

       return store;
}

int cmp(const void * a, const void *b)
{
      if ((int*)a<=(int*)b)  return 0;
      else return 1;
}

int selectPivotIndex(int left, int right)
{
      int median;
      median = (left+right)/2;
      if(median %2==0) median = median +1;
      return median;
}

Last edited by NevemTeve; 07-03-2023 at 02:48 AM.
 
1 members found this post helpful.
Old 07-19-2023, 04:15 AM   #21
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
In summary

Quote:
request vetting of the reason why "scanf("%d", ((int *)arr+i));" works, when "scanf("%d", ((int *)arr)[i]);" fails.
As mentioned by others:
the square brackets array\[\] automatically dereferences the array's contents, whereas array + i does not.
So the with the [] you get back an int

Code:
#include <stdio.h>

int main(void)
{
        int array[] = { 0,1,2,3,4,5};
        printf("%d\n", array[3]);
        printf("%d\n", *(array +3));

return 0;
}

Last edited by bigearsbilly; 07-19-2023 at 04:16 AM.
 
Old 07-22-2023, 05:49 AM   #22
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Your program is not unsalvable; please make an effort to understand the changes.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void do_qsort(void **ar, int(*cmp)(const void *, const void *), int left, int right);

static int cmp(const void *pa, const void *pb)
{
    int a= *(int *)pa;
    int b= *(int *)pb;
    if (a<b)       return -1;
    else if (a==b) return  0;
    else           return  1;
}

int main(){
    int *intvals = NULL; //array of integers
    int **vals= NULL;//array of pointers
    int total_elements;

    if (isatty(fileno(stdin))) {
        printf("How many elements in the array");
    }
    scanf("%d", &total_elements);

    intvals = malloc(total_elements*sizeof(int));
    vals = malloc(total_elements*sizeof(int *));
    for(int i= 0; i<total_elements; ++i) {
        vals[i]= &intvals[i];
        scanf("%d", vals[i]);
    }
    printf("unsorted:");
    for (int i=0; i<total_elements; ++i) {
        printf(" %d", *vals[i]);
    }
    fputc('\n', stdout);

    do_qsort((void **)vals, cmp, 0, total_elements-1);

    printf("sorted:");
    for (int i=0; i<total_elements; ++i) {
        printf(" %d", *vals[i]);
    }
    fputc('\n', stdout);

    free(vals);
    free(intvals);
    return 0;
}

int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int pivotIndex);

static void swap(void **arr, int i, int j) {
    void *tmp= arr[i];
    arr[i]= arr[j];
    arr[j]= tmp;
}

void do_qsort(void **ar, int(*cmp)(const void *, const void *), int left, int right){
    int pivotIndex;

    if (right<= left) return;
    else if (left+1==right) {
        if (cmp(ar[left], ar[right])>0) {
            swap(ar, left, right);
        }
        return;
    }

    pivotIndex = (left+right+1)/2;
    pivotIndex = partition(ar, cmp, left, right, pivotIndex);
    do_qsort(ar, cmp, left, pivotIndex-1);
    do_qsort(ar, cmp, pivotIndex+1, right);
}

int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int pivotIndex){
       int idx, store; void *pivot = ar[pivotIndex]; void *tmp = ar[right];
       ar[right] = ar[pivotIndex];  ar[pivotIndex]= tmp;
       store = left;
       for(idx= left; idx<right;idx++){
           if(cmp(ar[idx], pivot) <=0){
                  tmp = ar[idx]; ar[idx]=ar[store]; ar[store]=tmp; store++;
           }
       }
       tmp= ar[right];  ar[right] = ar[store];  ar[store] = tmp;

       return store;
}
Compiled on onlinegdb.com.

On sample data of 10 values, in input; as: 15, 1, 30, 2, 70, 3, 65, 3, 4, 91
wanted to see the actual execution, by modifying the code as shown below:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void do_qsort(void **ar, int(*cmp)(const void *, const void *), int left, int right, int counter);

static int cmp(const void *pa, const void *pb)
{
    int a= *(int *)pa;
    int b= *(int *)pb;
    if (a<b)       return -1;
    else if (a==b) return  0;
    else           return  1;
}

int main(){
    int *intvals = NULL; //array of integers
    int **vals= NULL;//array of pointers
    int total_elements;

    if (isatty(fileno(stdin))) {
        printf("How many elements in the array: ");
    }
    scanf("%d", &total_elements);

    intvals = malloc(total_elements*sizeof(int));
    vals = malloc(total_elements*sizeof(int *));
    for(int i= 0; i<total_elements; ++i) {
        vals[i]= &intvals[i];
        scanf("%d", vals[i]);
    }
    printf("unsorted:");
    for (int i=0; i<total_elements; ++i) {
        printf(" %d", *vals[i]);
    }
    fputc('\n', stdout);

    do_qsort((void **)vals, cmp, 0, total_elements-1, 0);

    printf("sorted:");
    for (int i=0; i<total_elements; ++i) {
        printf(" %d", *vals[i]);
    }
    fputc('\n', stdout);

    free(vals);
    free(intvals);
    return 0;
}

int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int pivotIndex);

static void swap(void **arr, int i, int j) {
    void *tmp= arr[i];
    arr[i]= arr[j];
    arr[j]= tmp;
}

void do_qsort(void **ar, int(*cmp)(const void *, const void *), int left, int right, int counter){
    int pivotIndex;
    printf ("\n ============ do_qsort() counter : %d, left: %d,  right: %d\n", counter, left, right);
    if (right<= left) return;
    else if (left+1==right) {
        if (cmp(ar[left], ar[right])>0) {
            swap(ar, left, right);
        }
        return;
    }
    
    pivotIndex = (left+right+1)/2;
    pivotIndex = partition(ar, cmp, left, right, pivotIndex);
    printf("\n counter: %d", counter);
    do_qsort(ar, cmp, left, pivotIndex-1, counter+1);
    do_qsort(ar, cmp, pivotIndex+1, right, counter+1);
}

int partition(void ** ar, int(* cmp)(const void*, const void*), int left, int right, int pivotIndex){
       int idx, store; 
       int current_elements = right - left +1;
       void *pivot = ar[pivotIndex]; void *tmp = ar[right];
       ar[right] = ar[pivotIndex];  ar[pivotIndex]= tmp;
       store = left;
       printf("partition: current: left: %d, right: %d, current_elements: %d::", left, right, current_elements);
       for (int i=0; i<current_elements; ++i)
                    printf(" %d", (int *)ar[i]); //Unable to get any meaningful output of it, as shown below 
       for(idx= left; idx<right;idx++){
           if(cmp(ar[idx], pivot) <=0){ printf("\n idx: %d, store: %d, left: %d, right: %d", idx, store, left, right);
                  tmp = ar[idx]; ar[idx]=ar[store]; ar[store]=tmp; store++;
           }
       }
       tmp= ar[right];  ar[right] = ar[store];  ar[store] = tmp;

       return store;
}
Q.1. Have confusion as what is the output implied by the line #84, 85; i.e.
Code:
for (int i=0; i<current_elements; ++i)
                    printf(" %d", (int *)ar[i]);
as that gives output as shown below. Say, on the first iteration, it gives the output as: -1508742464 -1508742460 -1508742456 -1508742452 -1508742448 -1508742428 -1508742440 -1508742436 -1508742432 -1508742444;
which though are the set of 10 values in the array for each of the 10 iterations, but cannot link to the actual elements in the array.
Code:
main.c: In function ‘partition’:
main.c:85:31: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
   85 |                     printf(" %d", (int *)ar[i]);
      |                              ~^   ~~~~~~~~~~~~
      |                               |   |
      |                               int int *
      |                              %ls

How many elements in the array: 10
15
1
30
2
70
91
65
3
4
3
unsorted: 15 1 30 2 70 91 65 3 4 3

 ============ do_qsort() counter : 0, left: 0,  right: 9
partition: current: left: 0, right: 9, current_elements: 10:: -1508742464 -1508742460 -1508742456 -1508742452 -1508742448 -1508742428 -1508742440 -1508742436 -1508742432 -1508742444
 idx: 0, store: 0, left: 0, right: 9
 idx: 1, store: 1, left: 0, right: 9
 idx: 2, store: 2, left: 0, right: 9
 idx: 3, store: 3, left: 0, right: 9
 idx: 4, store: 4, left: 0, right: 9
 idx: 5, store: 5, left: 0, right: 9
 idx: 6, store: 6, left: 0, right: 9
 idx: 7, store: 7, left: 0, right: 9
 idx: 8, store: 8, left: 0, right: 9
 counter: 0
 ============ do_qsort() counter : 1, left: 0,  right: 8
partition: current: left: 0, right: 8, current_elements: 9:: -1508742464 -1508742460 -1508742456 -1508742452 -1508742432 -1508742428 -1508742440 -1508742436 -1508742448
 idx: 0, store: 0, left: 0, right: 8
 idx: 1, store: 1, left: 0, right: 8
 idx: 2, store: 2, left: 0, right: 8
 idx: 3, store: 3, left: 0, right: 8
 idx: 4, store: 4, left: 0, right: 8
 idx: 5, store: 5, left: 0, right: 8
 idx: 6, store: 6, left: 0, right: 8
 idx: 7, store: 7, left: 0, right: 8
 counter: 1
 ============ do_qsort() counter : 2, left: 0,  right: 7
partition: current: left: 0, right: 7, current_elements: 8:: -1508742464 -1508742460 -1508742456 -1508742452 -1508742436 -1508742428 -1508742440 -1508742432
 idx: 1, store: 0, left: 0, right: 7
 idx: 3, store: 1, left: 0, right: 7
 idx: 4, store: 2, left: 0, right: 7
 idx: 5, store: 3, left: 0, right: 7
 counter: 2
 ============ do_qsort() counter : 3, left: 0,  right: 3
partition: current: left: 0, right: 3, current_elements: 4:: -1508742460 -1508742452 -1508742428 -1508742436
 idx: 0, store: 0, left: 0, right: 3
 idx: 1, store: 1, left: 0, right: 3
 idx: 2, store: 2, left: 0, right: 3
 counter: 3
 ============ do_qsort() counter : 4, left: 0,  right: 2
partition: current: left: 0, right: 2, current_elements: 3:: -1508742460 -1508742428 -1508742452
 idx: 0, store: 0, left: 0, right: 2
 counter: 4
 ============ do_qsort() counter : 5, left: 0,  right: 0

 ============ do_qsort() counter : 5, left: 2,  right: 2

 ============ do_qsort() counter : 4, left: 4,  right: 3

 ============ do_qsort() counter : 3, left: 5,  right: 7
partition: current: left: 5, right: 7, current_elements: 3:: -1508742460 -1508742452 -1508742428
 idx: 5, store: 5, left: 5, right: 7
 idx: 6, store: 6, left: 5, right: 7
 counter: 3
 ============ do_qsort() counter : 4, left: 5,  right: 6

 ============ do_qsort() counter : 4, left: 8,  right: 7

 ============ do_qsort() counter : 2, left: 9,  right: 8

 ============ do_qsort() counter : 1, left: 10,  right: 9
sorted: 1 2 3 3 4 15 30 65 70 91
Q.2. How to remove the warning obtained on compilation, in onlinegdb.com?

Last edited by ajiten; 07-22-2023 at 06:18 AM.
 
Old 07-22-2023, 06:24 AM   #23
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
- printf(" %d", (int *)ar[i]);
+ printf(" %d", *(int *)ar[i]);
 
1 members found this post helpful.
Old 07-22-2023, 11:03 AM   #24
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
You can go to https://cdecl.org/ to check some of these.

The advice here should also help:

https://c-faq.com/decl/cdecl1.html
 
Old 07-22-2023, 11:05 AM   #25
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by NevemTeve View Post
Code:
- printf(" %d", (int *)ar[i]);
+ printf(" %d", *(int *)ar[i]);
The reason it worked, must be that need to both type-cast the contents of array (being a pointer to a pointer to an integer), and the fact that ar[i] = *(ar+i); hence *ar[i] = **(ar +i).
But, that means that (int *)*ar[i] should work too.
But, that gives error:
Code:
main.c: In function ‘partition’:
main.c:85:42: warning: dereferencing ‘void *’ pointer
   85 |                     printf(" %d", (int *)*ar[i]);
      |                                          ^~~~~~
main.c:85:35: error: invalid use of void expression
   85 |                     printf(" %d", (int *)*ar[i]);
      |                                   ^
Please tell why?

Last edited by ajiten; 07-22-2023 at 11:24 AM.
 
Old 07-22-2023, 12:52 PM   #26
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,266
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by ajiten View Post
The reason it worked, must be that need to both type-cast the contents of array (being a pointer to a pointer to an integer), and the fact that ar[i] = *(ar+i); hence *ar[i] = **(ar +i).
But, that means that (int *)*ar[i] should work too...
You state that as if it were fact, but clearly it is not.

The error message is very precise, trust it.

This works because the pointer to void pointer is typecast before being dereferenced...

Code:
printf(" %d", *(int *)ar[i]);
This produces the error because the pointer to void pointer is first being dereferenced, then its value is being typecast...

Code:
printf(" %d", (int *)*ar[i]);
And the error tells you exactly that, and exactly where...

Code:
main.c:85:42: warning: dereferencing ‘void *’ pointer
   85 |                     printf(" %d", (int *)*ar[i]);
      |                                          ^~~~~~
The two expressions are not equivalent.

Last edited by astrogeek; 07-22-2023 at 12:57 PM.
 
1 members found this post helpful.
Old 07-22-2023, 01:49 PM   #27
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,863

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
the * itself is not a typecast but an operator. (int *) is a typecast.
 
1 members found this post helpful.
Old 07-22-2023, 02:51 PM   #28
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by astrogeek View Post
You state that as if it were fact, but clearly it is not.

The error message is very precise, trust it.

This works because the pointer to void pointer is typecast before being dereferenced...

Code:
printf(" %d", *(int *)ar[i]);
This produces the error because the pointer to void pointer is first being dereferenced, then its value is being typecast...

Code:
printf(" %d", (int *)*ar[i]);
And the error tells you exactly that, and exactly where...

Code:
main.c:85:42: warning: dereferencing ‘void *’ pointer
   85 |                     printf(" %d", (int *)*ar[i]);
      |                                          ^~~~~~
The two expressions are not equivalent.
Have confusion as what is the output given by the earlier code; i.e.
Code:
for (int i=0; i<current_elements; ++i)
                    printf(" %d", (int *)ar[i]);
i.e., on sample data of 10 values, in input as: 15, 1, 30, 2, 70, 3, 65, 3, 4, 91;
gives output on the first iteration as: -1508742464 -1508742460 -1508742456 -1508742452 -1508742448 -1508742428 -1508742440 -1508742436 -1508742432 -1508742444
 
Old 07-22-2023, 03:55 PM   #29
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That happens, when you print a pointer (int *) as an int (%d).
 
2 members found this post helpful.
Old 07-22-2023, 04:21 PM   #30
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,863

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
you ought to use printf "%p" for pointers, instead of %d (but you can try %x too, if you wish).
The pointer itself is the location (address) of the variable in the memory, therefore it is actually an integer (or long).
see for example here: https://www.man7.org/linux/man-pages/man3/printf.3.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to pass array as parameter, using void pointer, s.t. the contents too are accessed in the called function. ajiten Programming 19 06-18-2023 07:52 PM
pthread giving error: invalid conversion from ‘void* (*)(int*)’ to ‘void* (*)(void*)’ knobby67 Programming 4 05-05-2017 10:54 AM
read any given maze.txt into an internal dynamically allocated array of strings. jack555 Programming 4 04-27-2011 01:31 AM
In C, using qsort for dynamically allocated array ntmsz Programming 7 08-23-2005 10:33 AM
void foo(void) and void foo() lackluster Programming 9 02-15-2003 10:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration