C - Passing an array between functions gets different values - c

So I'm trying to write a function that'll search a word in a 2D bulk.
The function returns an (int) array of size [3] with values as the answer.
Here is my main() function:
void main() {
char bulk[L][L];
for (int i = 0; i < L; i++) {
for (int j = 0;j < L;j++)
scanf_s(" %c", &bulk[i][j]);
}
int *arr = search(&bulk, L, "bc");
printf("ARR: %d, %d, %d\n", arr[0], arr[1], arr[2]);
}
And here's the search() function:
int *search(char(*bulk)[L], int size, char *word) {
int arr[3] = { 0,0,0 };
int flag = 9;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (bulk[i][j] == *word) {
if (checkRight(bulk, i, j, word)) flag=0;
}
if (flag != 9) {
arr[0] = i;
arr[1] = j;
arr[2] = flag;
printf("ARR: %d, %d, %d\n", arr[0], arr[1], arr[2]);
return arr;
}
}
}
return arr;
}
The checkRight() function works well, it returns 0/1 for if the word exists to the right. The problem is that the two printf's are printing different values.
Output for search(): "ARR: 0,1,0".
Output for main(): "ARR: -858993460, -858993460, 0".
I assume it's pointer-related but I'm struggling with finding the problem. Any ideas?
Thanks a bunch!

return arr;
In here you are returning the address of the first value in the array, this address points to a temporary value inside the stack frame of the function search.
try to pass arr as a parameter or using "static int arr[3]" in order to make the array not temporary.

Related

Print descending array C

I have to create functions for print array, fill array witn descending numbers.
I created functions for printing array and creating descending array.But I faced with a problem.
If I use my own function printArray() it prints something unclear. Where is the problem, what i do wrong?
Please, help.
Here is the code in C. value - is value of array
Function for printing array:
void printArray (int arr[]){
int i;
printf("\n");
for(i = 0; i < value; i ++)
printf("%3d ", arr[i]);
}
Function for creating descending array:
int createDescendingArray(int a[])
{
int i;
printf("\nDescending array is created.\n");
for (i = value; i > 0; i--) {
a[i] = i;
}
printArray(a); // print of created array
}
Main function:
int main(){
int arr1[value]; //create new array
arr1[value] = createDescendingArray (arr1); //fill array with descending numbers
}
However when I don't use my print function in function createDescendingArray() and print it in Main funktion with standart method like this:
{int i;
for(i = 0; i < value; i++)
{
a[i]=i;
printf("%3d", a[i]);
}
}
It shows descending array as ascending (look at the picture)
How it works?
You have been using a variable named value in your function which prints array, without initializing it, hence the garbage value.
you should initialize it in the function or pass its start value as an argument to the function.
#include <stdio.h>
#include <stdlib.h>
void printArray(int *arr, int length)
{
int i;
printf("\n");
for (i = 0; i < length; i++)
{
printf("%3d ", arr[i]);
}
}
int *createDescendingArray(const int length)
{
if (length == 0)
return NULL;
int *a = malloc(length * sizeof(int));
;
printf("\nDescending array is created.\n");
for (int i = length-1; i >= 0; i--)
{
a[i] = i;
}
printArray(a, length); // print of created array
return a;
}
int main()
{
int *a = createDescendingArray(20);
printArray(a, 20);
return 0;
}
these changes should most probably do the trick but again, there is no initialization of value in the function that creates array as well
EDIT: stop creation of array if length is 0
EDIT2: fixed code to consider 0 as an element
EDIT3: Fixed code with suggestion from #CraigEstey in comments, tested and working
EDIT4: fixed for loop and removed cast on mallock
The function
int createDescendingArray(int a[])
{
int i;
printf("\nDescending array is created.\n");
for (i = value; i > 0; i--) {
a[i] = i;
}
printArray(a); // print of created array
}
is wrong.
According to the output in your question, it seems that you have defined value as 4 (you are not showing us the code with the definition). In that case, your code for the mentioned function is equivalent to the following:
int createDescendingArray(int a[])
{
printf("\nDescending array is created.\n");
a[4] = 4;
a[3] = 3;
a[2] = 2;
a[1] = 1;
printArray(a); // print of created array
}
I did nothing else to the code than unroll the loop.
Since the array a has a size of 4 elements, valid indices are from 0 to 3. Therefore, by writing to a[4], you are writing to the array out of bounds, causing undefined behavior.
If you had written
for (i = value - 1; i >= 0; i--)
instead of
for (i = value; i > 0; i--)
then the unrolled loop would be:
a[3] = 3;
a[2] = 2;
a[1] = 1;
a[0] = 0;
This is better, because now we have fixed the undefined behavior; you are no longer writing to the array out of bounds. However, this is still not what you want. If you want descending output, your unrolled loop must look like this instead:
a[0] = 3;
a[1] = 2;
a[2] = 1;
a[3] = 0;
This can be accomplished by changing your function to the following:
int createDescendingArray(int a[])
{
int i;
printf( "\nDescending array is created.\n" );
for ( i = 0; i < value; i++ ) {
a[i] = value - i - 1;
}
printArray(a); // print of created array
}
Here is a small test program:
#include <stdio.h>
//NOTE: It is customary for constants to be written upper-case,
//not lower-case, so the line below should normally not be used.
#define value 4
void printArray (int arr[]) {
int i;
printf( "\n" );
for( i = 0; i < value; i++ )
printf("%3d ", arr[i]);
}
int createDescendingArray(int a[])
{
int i;
printf( "\nDescending array is created.\n" );
for ( i = 0; i < value; i++ ) {
a[i] = value - i - 1;
}
printArray(a); // print of created array
}
int main( void )
{
int array[value];
createDescendingArray( array );
}
The output is:
Descending array is created.
3 2 1 0
In this test program, I took over most of your other code, but I did not take over the function main, because it was also causing undefined behavior:
int main(){
int arr1[value]; //create new array
arr1[value] = createDescendingArray (arr1); //fill array with descending numbers
}
In the line
arr1[value] = createDescendingArray (arr1);
you are assigning the return value of the function to a variable, although the function did not return a value. This causes undefined behavior. You may want to consider changing the return type to void in the function declaration, if it does not return a value.
Also, even if the function did return a value, arr1[value] would be writing to the array out of bounds, as valid indices are from 0 to value - 1.

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);

Arrays of pointer dereferencing

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);
}

Return malloc in C

int* dec2bin(int y){
int *arr = (int*)malloc(sizeof(int)*5);
int i;
for (i=0; i<5; i++) arr[i]=0;
return arr;
}
In this code, I write 0 to arr[0] through to arr[4], but the function returns 1070192. I want to return 00000.
What am I doing wrong?
Most probably you are printing the address returned by malloc().
You need a loop to print the contents, for example
int *dec2bin(int size)
{
return calloc(size, sizeof(int));
}
int main(void)
{
int *data;
int index;
int size;
size = 5;
data = dec2bin(size);
if (data == NULL)
return -1; /* allocation error */
for (index = 0 ; index < size ; index++)
printf("%d", data[index]);
printf("\n");
free(data);
return 0;
}
You can see that I used calloc(), that's because you are going to initialize all the values to 0, otherwise use malloc() instead.
The function returns pointer to the allocated memory
int* dec2bin(int y){
int *arr = (int*)malloc(sizeof(int)*5);
int i;
for (i=0; i<5; i++) arr[i]=0;
return arr;
}
for an array of 5 integers. Its address may not be equal to 0 unless the allocation failed. But the integers themselves are equal to 0.
If you will write the call for example the following way (it is not clear the meaning of the parameter)
int *p = dec2bin( 0 );
then *p, the first element of the array, will be indeed equal to 0
Or even you can write
int *p = dec2bin( 0 );
for ( int i = 0; i < 5; i++ ) printf( "%i ", p[i] );
and you will get that all elements of the array are equal to 0.
If you would like to get value of that array as a string (array of characters), below is the code you can start off by.
char* dec2bin(int y) {
int *arr = (int *) malloc(sizeof(int) * 5);
char output[50];
int i;
for (i=0; i<5; i++) arr[i] = 0;
sprintf(output, "%d%d%d%d%d", arr[0], arr[1], arr[2], arr[3], arr[4]);
return output;
}
For printing it out, you could use:
char *string = dec2bin(0);
printf("%s", string);

creating/printing array using pointers in C

So for some Uni work I need to create an array using a function (my first time with C functions and pointers) but store the array as a pointer because i dont think C can use arrays in functions? And then also use another function to print out each element in the array. The code i use in main is:
int* x = get_lotto_draw();
print_array(x);
And then my functions are:
int* get_lotto_draw() //Returns an array of six random lottery numbers 1-49
{
int min = 1;
int max = 49;
int counter = 0;
srand(time(NULL));
int r = rand()%(max-min)+min;
int *arrayPointer = malloc(6 * sizeof(int));
for(counter = 0; counter <= 5; counter++)
{
arrayPointer[counter] = r;
}
return arrayPointer;
}
void print_array(int * array) //Print out the content of an array
{
int i = 0;
int printerArray[6] = {0, 0, 0, 0, 0, 0};
for(i = 0; i <= 5; i++)
{
printerArray[i] = array[i];
}
printf("array = %d", array);
printf("printerArray = %d", printerArray);
for(i = 0; i <= 5; i++)
{
printf("Array element %d : %d\n", i, printerArray[i]);
}
}
But im doing something wrong, and either the array isnt getting created correctly, or the print isnt working correctly. Thanks for your time.
What you want is:
void print_array(int * array) //Print out the content of an array
{
int i = 0;
for(i = 0; i <= 5; i++)
{
printf("Array element %d : %d\n", i, array[i]);
}
}
Following two lines could provoke undefined behavior
printf("array = %d", array);
printf("printerArray = %d", printerArray);
you can't use %d here, as array and printerArray decays to pointers in this context and in order to print pointer you should use %p and cast your arrays to void * (thanks to #user3447428 for his comment about cast )

Resources