Jump to content

Pointers C (**)

Go to solution Solved by Mr_KoKa,

Ok so it will be


		*(*firstArray+i)=array[i];// here

As you re pasing pointer at pointer so first you need to get pointer value from that poitner then shift it and then dereference(?) what it points to

 

I had to make a function that splits array data into two arrays.

What i don't understand is this bit:

(*firstArray)[i]=array[i]

 

why we can write like this, but not like *(firstArray+i) ?

 

only the important snippet of code below:

int main()
{
  int *firstArray=NULL;
  
  splitData(&array);
}

int splitData(**array)
{
	int someArray[]={0,0,0,0,0};
  int firstArraySize=7;// not the point
 *firstArray = malloc(sizeof(int) * firstArraySize);
  
  for(int i=0; i<firstArraySize; i++)
	{
		(*firstArray)[i]=someArray[i];// here
	}
}

 

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/686223-pointers-c/
Share on other sites

Link to post
Share on other sites

4 minutes ago, Mr_KoKa said:

I would say you can, have you tried?

i get:

assignment makes pointer from integer without a cast

*(firstArray+i) = array[ i ];

and values are jacked up

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/686223-pointers-c/#findComment-8812063
Share on other sites

Link to post
Share on other sites

Spoiler

#include <stdio.h>
#include <stdlib.h>

int splitData(int *array, int size, int firstArraySize, int **firstArray, int **secondArray)
{
	if(*firstArray!=NULL || *secondArray!=NULL || firstArraySize>=size || size<2)
	return -1;
		
	*firstArray = malloc(sizeof(int) * firstArraySize);// here
	*secondArray = malloc(sizeof(int) * (size-firstArraySize));
	
	if(*firstArray==NULL || *secondArray==NULL)
	return -1;
	
	for(int i=0; i<firstArraySize; i++)
	{
		(*firstArray)[i]=array[i];// here
	}
	int count=0;
	
	for(int i=firstArraySize; i<size; i++)
	{
		(*secondArray)[count]=array[i];
		count++;
	}

	
	return 0;
}
int main()
{
	
	int array[]={0,1,2,3,4,5,6,7,8,9};
	int size=sizeof(array)/sizeof(int);
	int firstArraySize=7;
	int *firstArray=NULL;// here
	int *secondArray=NULL;
	
	printf("%d \n",splitData(array,size,firstArraySize,&firstArray,&secondArray));
	
	for(int i=0; i<firstArraySize; i++)
	{
		printf("%d ", firstArray[i] );// here
	}
	printf("\n");
	int count=0;
	for(int i=firstArraySize; i<size; i++)
	{
		printf("%d ",secondArray[count]);
		count++;
	}
	free(firstArray);
	free(secondArray);
	
	printf("\n");
	return 0;
}

 

maybe you'll be able to see something from full code

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/686223-pointers-c/#findComment-8812090
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×