Arrays of pointer dereferencing - c

I want to a function named sortPointers() that sets an array of integer pointers to point to the elements of another array in ascending order.
What I have done so far is
void sortP(int src[], int *ptrs[], int n)
{
int temp;
for(int i = 0; i< n ; i++)
{
ptrs[i] = & src[i]; // assign the address of each number in the src[] to the array of pointers
}
while (1)
{
int flag = 0;
for(int i = 0; i< n;i++)
{
if ( *(ptrs[i]) > *(ptrs[i+1])) //bubble sort
{
temp = *(ptrs[i]);
*(ptrs[i]) = *(ptrs[i+1]);
*(ptrs[i+1]) = temp;
flag = 1;
}
}
if(flag == 0);
break;
}
for(int i = 0; i< n;i++)
{
printf("%i\n",ptrs[i]);
}
}
In main function , I call this function
main()
{
int a[5] = {5,4,3,2,1};
int *ptrs[5]= {&a[0],&a[1],&a[2],&a[3],&a[4]};
sortP(a, *ptrs, 5);
}
My result are addresses, If I want to print out the actual value that the pointers point to (1,2,3,4,5) ,what should I change in the printf()?
THanks
P.S. I try *ptrs[i] before , but I got strange number though , not the ones in src[]..

My result are addresses
Technically, your results are undefined behavior, because %i expects an int, not an int*.
Fixing this problem is simple: add a dereference operator in front of ptrs[i], like this:
for(int i = 0; i< n;i++) {
printf("%i\n", *ptrs[i]);
}
I got strange number though , not the ones in src[]
The real problem with your code is that you are swapping pointers incorrectly. In fact, you can tell that it's incorrect simply by looking at temp: it needs to be int*, not int and the dereferences on the swap need to go away.

see annotations :
void sortP(int src[], int *ptrs[], int n)
{
int temp;
for(int i = 0; i< n ; i++)
{
ptrs[i] = & src[i]; // assign the address of each number in the src[] to the array of pointers
}
while (1)
{
int flag = 0;
// check if i < n-1, not n
for(int i = 0; i< n-1;i++)
{
if ( *(ptrs[i]) > *(ptrs[i+1])) //bubble sort
{
temp = *(ptrs[i]);
*(ptrs[i]) = *(ptrs[i+1]);
*(ptrs[i+1]) = temp;
flag = 1;
}
}
if(flag == 0)
break;
}
for(int i = 0; i< n;i++)
{
//*ptrs[i] instead of ptrs[i]
printf("%i ",*ptrs[i]);
}
}
int main(void)
{
int a[5] = {5,4,3,2,1};
int *ptrs[5];//= {&a[0],&a[1],&a[2],&a[3],&a[4]};
sortP(a, ptrs, 5);
}

Related

Why do I get a segmentation fault by declaring a 2d array in c?

I am new to threads and I have a program that uses threads to find the minimum number out of a 2d array and later on, it finds the distance that the other elements of the array have from the minimum number and stores them in another array.
The user should enter the size of the array and the number of threads he wants to use.
I tried the program below for 1d array and it worked just fine. When I converted it to work for a 2d array it started crashing and throwing a segmentation fault. I, however, cannot find which part of the 2d declaration is wrong.
Any help is really appreciated.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <pthread.h>
struct Parameters
{
// input
int s,p; //n is size of array, p is number of threads
int** array; //array with elements
int start;
int end;
// output
int smallest;
int pos; //position if minimum
int** B; //array that holds the distances
};
void* min(void* args)
{
struct Parameters* p = (struct Parameters*)args;
int **array = p->array;
int **B1 = p->B;
int start = p->start;
int end = p->end;
int smallest = array[start][start];
int pos = p->pos;
int distance;
//find the smallest
for (int i = start; i < end; i++)
{
for(int j = start; j < end; j++)
{
if (array[i][j] < smallest)
{
smallest = array[i][j];
pos = i;
}
}
}
//find the distances
for(int i = 0; i < ((struct Parameters*)args) -> s; i++)
{
for(int j = 0; j < ((struct Parameters*)args) -> s; j++)
{
distance = abs(pos - i);
B1[i][j] = distance;
}
}
params->smallest = smallest;
params->B = B1;
return NULL;
}
int main()
{
int smallest,pos;
int s,p;
struct Parameters *ptr = (struct Parameters *)malloc(sizeof(struct Parameters));
if(ptr == NULL)
{
printf("Not enough. Try again \n");
exit(0);
}
printf("Type s\n");
scanf("%d",&(ptr->s));
printf("Type p\n");
scanf("%d", &(ptr->p));
// declare an array of threads and associated parameter instances
pthread_t threads[(ptr->p)];
struct Parameters thread_parameters[(ptr->p)] ;
int arr[ptr->s][ptr->s];
int B2[ptr->s][ptr->s];
// intialize the array
for(int i=0; i< ptr->s; i++)
{
for(int j=0; j< ptr->s; j++)
{
printf("Type a \n");
scanf("%d",&arr[i][j]);
}
}
// smallest needs to be set to something
smallest = arr[0][0];
// start all the threads
for (int i = 0; i < ptr->p; i++)
{
memcpy(arr, thread_parameters[i].array, sizeof(arr));
thread_parameters[i].s = ptr->s;
memcpy(Bb, thread_parameters[i].B, sizeof(B2));
thread_parameters[i].start = i * (ptr->s / ptr->p);
thread_parameters[i].end = (i+1) * (ptr->s / ptr->p);
pthread_create(&threads[i], NULL, min, &thread_parameters[i]);
}
// wait for all the threads to complete
for (int i = 0; i < ptr->p; i++)
{
pthread_join(threads[i], NULL);
}
// Now aggregate the "smallest" and "largest" results from all thread runs
for (int i = 0; i < ptr->p; i++)
{
if (thread_parameters[i].smallest < smallest)
{
smallest = thread_parameters[i].smallest;
}
}
printf("Smallest is %d\n", smallest);
thread_parameters[ptr->p].B[ptr->s][ptr->s];
for (int i = 0; i < 1; i++)
{
for(int j = 0; j < ptr->s;j++)
{
for(int k = 0; k < ptr->s; k++)
{
printf("Element %d is %d away from min\n",j,thread_parameters[i].B[j][k]);
}
}
}
return 0;
}
Thank you!!
The issue with your code might also come from :
memcpy(arr, thread_parameters[i].array, sizeof(arr));
...
memcpy(Bb, thread_parameters[i].B, sizeof(B2));
as thread_parameters[i].array and thread_parameters[i].B are not allocated, if you are only reading the array it might b fine to only pass them by address
thread_parameters[i].array = arr
but for thread_parameters[i].B you would need to allocate the arrays and perform a deep copy (memcpy would not work)
The below text does not answer the question but does provide some insight on VLA usage
One reason for causing the segmentation with a declaration of a Variable Length Array is that the value is to large to allocate the array on the stack (some compiler choose this option, this choice might have performance reason).
The is not much option to recover cleanly from failure to allocate memory on the stack as there is little way to clean up stack memory during runtime within the same stack context.
You can mitigate the issue by allocating your 2D arrays on the heap instead, some of the strategies are available here(thanks #Lundin) and here.
int** alloc_2d_int_array(size_t rows, size_t cols) {
int **result = malloc(rows * sizeof(int *));
if(result == NULL) {
// could not allocate more memory
return NULL;
}
size_t row_size = cols * sizeof(int);
for(int i=0; i < rows; ++i) {
result[i] = malloc(row_size);
if(result[i] == NULL) {
// could not allocate more memory
// cleanup
return NULL;
}
}
return result;
}
the above implementation have not been tested, but does compile, there are still risk of integer overflow.
Then use the above define function as following:
int **arr = alloc_2d_int_array(ptr->s, ptr->s);
int **B2 = alloc_2d_int_array(ptr->s, ptr->s);
easier implementation (see here(thanks #Lundin))
int **arr = malloc(sizeof(int[ptr->s][ptr->s]);
int **B2 = malloc(sizeof(int[ptr->s][ptr->s]);

Count greater elements on right side of a current element in an array

I am trying to count the number of elements greater than the element on the right side of the array. Here my function goes.
int* SurpassersKing(int input1_size, int* input1,int* output_size)
{
int i,k;
int count[input1_size];
for (i = 0; i < input1_size; i++)
count[i] = 0;
for ( i = 0; i < input1_size; i++)
{
for ( k = i + 1; k <input1_size; k++)
{
if (input1[i] < input1[k]) {
count[i]++;
}
}
}
return count;
}
This is my function where I am counting greater elements in an array.
So in this following code snippet i have wriiten the main function , declaring all the veriable like output_size,counting array ,i ,k as an index for the arrays and printing the stuff , and calling counting function .
int main() {
int output_size;
int* output;
int ip1_size = 0;
int ip1_i;
scanf("%d\n", &ip1_size);
int ip1[ip1_size];
for(ip1_i = 0; ip1_i < ip1_size; ip1_i++) {
int ip1_item;
scanf("%d", &ip1_item);
ip1[ip1_i] = ip1_item;
}
output = SurpassersKing(ip1_size,ip1,&output_size);
int output_i;
for(output_i=0; output_i < output_size; output_i++) {
printf("%d\n", output[output_i]);
}
return 0;
}
but i am not getting the output required so what can i do to improve this.
Your logic to calculate the count of the numbers to the right side is correct. Only problem is you can't return arrays like that from a function. Try following:
Replace
int count[input1_size];
with
int * count = malloc(input1_size*sizeof(int));
And then in the main function
Add
free(output);
just before the return statement.
Oso your code has a few errors i found when i tried to compile.
This is a c code so use #include
Inside the function SurpassersKing you are trying to return array count which is not allowed. Never return local variables unless it is dynamically created.
The output_size never gets initilized.
This is the final code:
#include <stdio.h>
#include<stdlib.h>
int* SurpassersKing(int input1_size, int* input1)
{
int i,k;
int * count = (int*)malloc(input1_size*sizeof(int));
for (i = 0; i < input1_size; i++)
*(count + i) = 0;
for ( i = 0; i < input1_size; i++)
{
for ( k = i + 1; k <input1_size; k++)
{
if (input1[i] < input1[k]) {
count[i]++;
}
}
}
return count;
}
int main() {
// your code goes here
int output_size;
int* output;
int ip1_size = 0;
int ip1_i;
int output_i;
printf("Enter the size:\n");
scanf("%d",&ip1_size);
int ip1[ip1_size];
for(ip1_i = 0; ip1_i < ip1_size; ip1_i++) {
scanf("%d",%ip1[ip1_i]);
}
output = SurpassersKing(ip1_size,ip1);
output_size = ip1_size;
for(output_i=0; output_i < output_size; output_i++) {
printf("%d\n",output[output_i]");
}
return 0;
}

Combining two different arrays using pointers?

I have a function and inside it, I have two arrays and two loops.
First one is an array of characters and the second one is an array of pointers with NULL values. In the first loop, I set values to null.
void printName(char *pointer, int size)
{
char arrayOfChars[10] = "test";
char *arrayOfPointers[10]
// set values to null
for(int i = 0; i < arrayOfPointers; i++)
{
arrayOfPointers[i] = NULL;
}
int j = 0;
do
{
printf("Value: %d: %p\n",arrayOfChars[j], arrayOfPointers+j);
j++;
}
while(j < 10);
}
so what I am trying to achieve is something like this:
value: t : 0028FEF
value: e : 0028FEY
value: s : 0028FEX
value: t : 0028FEE
I'm going to do what I think you want, rather than what I think you're asking for:
void printName(char *pointer, int size)
{
for (i = 0; i < size; ++i)
printf("Value: %d, addr: %p\n", pointer[i], pointer + i);
}
You mean:
for(int i = 0; i < 10; i++)
{
arrayOfPointers[i] = &arrayOfChars[i];
}
followed by:
int j = 0;
do
{
printf("Value: %d: %p\n",arrayOfChars[j], arrayOfPointers[j]);
j++;
}
while(j < 10);

C Fibonacci Sequence in Array Segmentation Fault

I want to fill an array up to n digits of the Fibonacci sequence. Here's what I have so far:
void fib(int **seq, int n){
seq = malloc(sizeof(int)*n);
if(n==1){
*seq[0] = 0;
}
if(n==2){
*seq[0] = 0;
*seq[1] = 1;
}
if(n>=3){
*seq[0] = 0;
*seq[1] = 1;
*seq[2] = 1;
for(int i=3; i<n; i++){
*seq[i] = *seq[i-1]+*seq[i-2];
}
}
}
I've tested the basic algorithm in Python and it works as expected, but in C I'm getting a segmentation fault and I'm not sure where that is coming from.
Any help would be appreciated.
Your program is correct on most parts except for the following errors:
(1) Repalce
seq = malloc(sizeof(int)*n);
with
*seq = malloc(sizeof(int)*n);
Note that you should not be allocating memory to the double pointer. You should be allocating the memory to the pointer pointed by 'seq'.
(2) Replace all the statements like
*seq[i]...
with
(*seq)[i]
Note that [] has a stronger affinity than *. So you need to surround *seq with parenthesis on all the instances.
(3) You haven't shown how you invoke this function. But I am assuming that you are allocating memory to the double pointer before calling the function.
Here's a working program for your reference:
#include <stdio.h>
void fib(int **seq, int n);
int main()
{
int ** sequence = malloc(sizeof(int*));
fib(sequence, 5);
for(int x = 0; x < 5; x++)
printf("%d", (*sequence)[x]);
return 0;
}
void fib(int **seq, int n){
*seq = malloc(sizeof(int)*n);
if(n==1){
(*seq)[0] = 0;
}
if(n==2){
(*seq)[0] = 0;
(*seq)[1] = 1;
}
if(n>=3){
(*seq)[0] = 0;
(*seq)[1] = 1;
(*seq)[2] = 1;
for(int i=3; i<n; i++){
(*seq)[i] = (*seq)[i-1]+(*seq)[i-2];
}
}
}
You are passing pointer of pointer that is void fib(int **seq, int n)
than try to allocate memory seq = malloc(sizeof(int)*n); which is wrong
Instead of doing this you can simply do
void fib(int *seq, int n){
if(n==1){
seq[0] = 0;
}
if(n==2){
seq[0] = 0;
seq[1] = 1;
}
if(n>=3){
seq[0] = 0;
seq[1] = 1;
seq[2] = 1;
for(int i=3; i<n; i++){
seq[i] = seq[i-1]+seq[i-2];
}
}
}
int main()
{
int n=10;
int *arr=(int*)malloc(sizeof(int)*(n)); // Allocate memory here
fib(arr,10);
for(int i=0;i<10;i++)
cout<<arr[i]<<" ";
return 0;
}
You have to understand this :
for arrays:-
a[i] = *(a+i) // correct
so
*a[i] = **(a+i) //pointer to pointer so it will not work in your case

assigned value to dynamic array created in void function doesnt return the same value in main()

I have a problem with dynamic arrays in C. My program was working perfectly, but I was asked to put the creation of dynamic array into a seperate void. I did it, and it still worked great, but then I had to assign a value to a certain point of the created array in void, and make it return the said value, however, what I get is a random value. The function works by sending a pointer and the lenght of required array into void, and then makes the pointer into a dynamic array.
#include <stdio.h>
#include <stdlib.h>
#define MAX 255
void ieskom (int skaiciai[],int n, int *de, int *me, int *n1, int *n2)
{
int i = 0;
int j = 0;
int nr1 = 0;
int nr2 = 0;
int temp = 0;
int temp1 = 0;
int eile = 0;
int eile1 = 0;
int *did;
did = (int*)calloc(n,sizeof(int));
if (did==NULL)
{
printf("Nepriskirta atminties.");
exit(0);
}
int *maz;
maz = (int*)calloc(n,sizeof(int));
if (maz==NULL)
{
printf("Nepriskirta atminties.");
exit(0);
}
i = 0;
for (i = 0; i < n; i++)
{
if (skaiciai[i] < skaiciai[i+1])
{
did[j] = did[j] + 1;
if (did[j] > temp)
{
eile = j;
temp = did[j];
nr1 = i+1;
}
}
else
{
did[j] = did[j] + 1;
if (did[j] > temp)
{
eile = j;
temp = did[j];
nr1 = i+1;
}
j = j + 1;
}
}
j = 0;
for (i = 0; i < n; i++)
{
if (skaiciai[i] > skaiciai[i+1])
{
maz[j] = maz[j] + 1;
if (maz[j] > temp1)
{
eile1 = j;
temp1 = maz[j];
nr2 = i+1;
}
}
else
{
maz[j] = maz[j] + 1;
if (maz[j] > temp1)
{
eile1 = j;
temp1 = maz[j];
nr2 = i+1;
}
j = j + 1;
}
}
*de = did[eile];
*me = maz[eile1];
*n1 = nr1;
*n2 = nr2;
free(did);
free(maz);
}
/*int masyvas(x)
{
int y;
y = (int*)malloc(x*sizeof(int));
return y;
}*/
void *masyvas (int *skaiciai, int n)
{
*skaiciai = (int*)malloc(n*sizeof(int));
skaiciai[2] = 5;
return skaiciai;
}
int main()
{
int n1 = 0;
int n2 = 0;
int de = 0;
int me = 0;
int i = 0;
int n = 0;
int *skaiciai;
scanf("%d", &n);
// skaiciai = masyvas(n); // naudojant int
masyvas(&skaiciai, n);
printf("2 = %d", skaiciai[2]);
if (skaiciai==NULL)
{
printf("Nepriskirta atminties.");
exit(0);
}
for (;i < n; i++)
{
scanf("%d", &skaiciai[i]);
}
ieskom (skaiciai, n, &de, &me, &n1, &n2);
if (de > me)
{
printf("Elementu numeriai:");
printf(" %d", n1-de+1);
printf(" %d\n", n1);
printf("\nAtstumas tarp ju: %d", de-2);
}
else
{
printf("Elementu numeriai:");
printf(" %d", n2-me+1);
printf(" %d\n", n2);
printf("\nAtstumas tarp ju: %d", me-2);
}
free(skaiciai);
getchar();
getchar();
return 0;
}
The problem is in void masyvas and printf skaicia[2] - I assign a certain value to skaiciai[2], yet it prints a random one. How do I fix it?
EDIT: Thank you for your answers and explanations, it really helped me a lot! I know have solved my problem, and most importantly, I know why it was a problem in the first place.
First of all, you should translate variables and texts to english (your code lack of comments, this should apply to them too).
Next your masyvas() function returns a pointer to the allocated array (why void* ?!) but when you call it you don't get the returned value.
You have to choose: either you pass a pointer to your function (an array is a pointer, to if you want an array to be allocated from a function you have to pass a pointer to the pointer, so a int **), or you use the returned value.
Allocating with returned value:
// this function allocates a int* tab of size n and set one value
int *allocate_tab(int n) {
int *tmp;
tmp = malloc(n*sizeof(int));
if (tmp == NULL) {
return(NULL); // failed
}
tmp[2] = 5;
return(tmp);
}
// in main (or other function)
int *mytab;
mytab = alloc_tab(45);
Allocating by passing a pointer to the array:
void alloc_tab(int **tab, int n) {
*tab = malloc(n*sizeof(int));
if (*tab == NULL) {
return;
}
(*tab)[2] = 5;
}
// in main (or other)
int *mytab;
alloc_tab(&mytab, 45);
If you can't understand this stuff I guess you should read more about memory, allocation and pointers.
You need to pass a pointer-to-pointer here and do not need to return anything.
void masyvas (int **skaiciai, int n)
{
*skaiciai = (int*)malloc(n*sizeof(int));
(*skaiciai)[2] = 5;
}
When you declare int *skaiciai, the variable is a pointer to type int. skaiciai holds the address that points to an int. When you pass &skaiciai, you're passing the address of the address that points to an int. So because this is an address of an address, its a double pointer.

Resources