I'm having a hard time using pass by reference - c

I don't know why I keep on getting errors in my code when I'm trying to do pass-by-reference, for finding the largest number of an integer using recursion.
My code works when it's pass-by-value, but I fail to do it correctly in pass-by-reference.
My main:
#include <stdio.h>
#include <math.h>
void largest_digit(int* digit);
int main() {
int num;
printf("\nPlease enter a number ");
scanf("%d", &num);
largest_digit(&num);
printf("The largest digit = %d\n", num);
return 0;
}
My function:
void largest_digit(int* digit) {
int hold, hold1;
if(*digit == 0 || *digit < 0) {
*digit = 0;
*digit;
return;
}
// If-statement with Recursion.
if(*digit > 0){
hold = *digit % 10;
hold1 = largest_digit(*digit/10);
if(hold > hold1) {
hold = *digit;
*digit;
return;
} else {
hold1 = *digit;
*digit;
return;
}
}
}

As someone said before, the largest_digit function is void, so it can't be assinged to a variable when is called. What you can do instead, is modifying *digit before the call, and then assign the value of *digit to what you want, in this case hold1.
Other thing is that you need to save the value into *digit before returning, for example, instead of doing hold = *digit, you should do *digit = hold.
Here are the suggested modifications:
void largest_digit(int* digit) {
int hold, hold1;
if(*digit == 0 || *digit < 0) {
*digit = 0;
return;
}
// If-statement with Recursion.
if(*digit > 0){
hold = (*digit) % 10;
(*digit) /= 10;
largest_digit(digit);
hold1 = *digit;
if(hold > hold1) {
*digit = hold;
return;
} else {
*digit = hold1;
return;
}
}
}
With this main,
int main() {
int a=123, b=652, c=3274;
largest_digit(&a);
largest_digit(&b);
largest_digit(&c);
printf("%d\n",a);
printf("%d\n",b);
printf("%d\n",c);
return 0;
}
the output is
3
6
7

You said you were passing it by reference, but you just tried to pass it by value here
hold1 = largest_digit(*digit/10);
Create a new int with *digit/10 and pass the address to largest_digit
int hold1Temp = *digit/10;
hold1 = largest_digit(&hold1Temp);
EDIT: Your function should be something like this:
void largest_digit (int* digit)
{
if (*digit <= 0) return; // if there's no more digit to compare, stop
int currentDigit = *digit % 10; // if you receive 982, it gets the 2
int nextDigit = *digit/10; // if you receive 982, it turns into 98
largest_digit(&nextDigit); // does the same thing again
*digit = currentDigit > nextDigit ? currentDigit : nextDigit; // chooses the biggest digit
}

A couple of things first:
The unary indirection operator (*) used on a pointer means "look at what is this pointing to". Therefore, the statement *digit; alone is not useful to anything. You can very well remove it from your code (I see you use it multiple times), perhaps you meant to do an assignment? The statement *digit = X; is an assignment and modifies the data pointed by the pointer.
"Passing by reference" does not exist in C. You can only pass by value. That value though can be a pointer to another value, that is how you "simulate" passing something by reference.
A function declared as void f(...) does not return any value. Therefore, assigning the "return value" of such a function to a variable does not make sense.
Now, considered the above:
Your call largest_digit(*digit/10) is not passing a pointer, but dereferencing the pointer digit, dividing the value by 10, and then passing that as parameter. As you already figured, this is wrong. To correctly pass by reference in your case, you would need to either modify the original value pointed to by digit, or create a new one and pass its address.
In any case, passing a pointer around (instead of the value directly) for this kind of recursive function does not make much sense and is only a complicated twist that does not accomplish much other than making your life harder. Use a plain value as argument.
int largest_digit(int num) {
if (num < 0)
return largest_digit(-num);
if (num == 0)
return 0;
int cur = num % 10;
int next = largest_digit(num / 10);
if (cur > next)
return cur;
return next;
}
int main(void) {
printf("%d\n", largest_digit(1923)); // 9
printf("%d\n", largest_digit(4478)); // 8
printf("%d\n", largest_digit(-123)); // 3
}
NOTE: for simplicity, the above function also handles negative numbers by calling largest_digit(-num) if the number is negative, therefore it only supports negative digits down to INT_MIN+1 (that is, it does not correctly handle INT_MIN).

Your trouble is that other than the case where *digit is negative, you never actually set *digit. Each time you do this:
*digit;
The above only dereferences the pointer and looks up the value, but it doesn't actually change it. What you need to do on each of your return routes is to actually set the value to something:
*digit = ...something...;
Without setting this value anywhere, the value of num in your main() function is never actually going to change.
In addition, you are treating largest_digit as if it has a return value, which it does not:
hold1 = largest_digit(*digit/10); // <- assigning the return value does not make sense

Related

last number in a function array

I want to write a function where I have a given array and number N. The last occurrence of this number I want to return address. If said number cannot be found I want to use a NULL-pointer
Start of the code I've made:
int main(void) {
int n = 3;
int ary[6] = { 1,3,7,8,3,9 };
for (int i = 0; i <= 6; i++) {
if (ary[i] == 3) {
printf("%u\n", ary[i]);
}
}
return 0;
}
result in command prompt:
3
3
The biggest trouble I'm having is:
it prints all occurrences, but not the last occurrence as I want
I haven't used pointers much, so I don't understand how to use the NULL-pointer
I see many minor problems in your program:
If you want to make a function, make a function so your parameters and return types are explicit, instead of coding directly in the main.
C arrays, like in most languages, start the indexing at 0 so if there are N element the first has index 0, then the second has 1, etc... So the very last element (the Nth) has index N-1, so in your for loops, always have condition "i < size", not "i <= size" or ( "i <= size-1" if y'r a weirdo)
If you want to act only on the last occurence of something, don't act on every. Just save every new occurence to the same variable and then, when you're sure it was the last, act on it.
A final version of the function you describe would be:
int* lastOccurence(int n, int* arr, int size){
int* pos = NULL;
for(int i = 0; i < size; i++){
if(arr[i] == n){
pos = &arr[i]; //Should be equal to arr + i*sizeof(int)
}
}
return pos;
}
int main(void){
int n = 3;
int ary[6] = { 1,3,7,8,3,9 };
printf("%p\n", lastOccurence(3, ary, 6);
return 0;
}
Then I'll add that the NULL pointer is just 0, I mean there is literally the line "#define NULL 0" inside the runtime headers. It is just a convention that the memory address 0 doesn't exist and we use NULL instead of 0 for clarity, but it's exactly the same.
Bugs:
i <= 6 accesses the array out of bounds, change to i < 6.
printf("%u\n", ary[i]); prints the value, not the index.
You don't actually compare the value against n but against a hard-coded 3.
I think that you are looking for something like this:
#include <stdio.h>
int main(void)
{
int n = 3;
int ary[6] = { 1,3,7,8,3,9 };
int* last_index = NULL;
for (int i = 0; i < 6; i++) {
if (ary[i] == n) {
last_index = &ary[i];
}
}
if(last_index == NULL) {
printf("Number not found\n");
}
else {
printf("Last index: %d\n", (int)(last_index - ary));
}
return 0;
}
The pointer last_index points at the last found item, if any. By subtracting the array's base address last_index - ary we do pointer arithmetic and get the array item.
The cast to int is necessary to avoid a quirk where subtracting pointers in C actually gives the result in a large integer type called ptrdiff_t - beginners need not worry about that one, so just cast.
First of all, you will read from out of array range, since your array last element is 5, and you read up to 6, which can lead in segmentation faults. #Ludin is right saying that you should change
for (int i = 0; i <= 6; i++) // reads from 0 to 6 range! It is roughly equal to for (int i = 0; i == 6; i++)
to:
for (int i = 0; i < 6; i++) // reads from 0 to 5
The last occurrence of this number I want to return as address.
You are printing only value of 3, not address. To do so, you need to use & operator.
If said number cannot be found I want to use a NULL-pointer
I don't understand, where do you want to return nullpointer? Main function can't return nullpointer, it is contradictory to its definition. To do so, you need to place it in separate function, and then return NULL.
If you want to return last occurence, then I would iterate from the end of this array:
for (int i = 5; i > -1; i--) {
if (ary[i] == 3) {
printf("place in array: %u\n", i); // to print iterator
printf("place in memory: %p\n", &(ary[i])); // to print pointer
break; // if you want to print only last occurence in array and don't read ruther
}
else if (i == 0) {
printf("None occurences found");
}
}
If you want to return an address you need yo use a function instead of writing code in main
As you want to return the address of the last occurence, you should iterate the array from last element towards the first element instead of iterating from first towards last elements.
Below are 2 different implementations of such a function.
#include <stdio.h>
#include <assert.h>
int* f(int n, size_t sz, int a[])
{
assert(sz > 0 && a != NULL);
// Iterate the array from last element towards first element
int* p = a + sz;
do
{
--p;
if (*p == n) return p;
} while(p != a);
return NULL;
}
int* g(int n, size_t sz, int a[])
{
assert(sz > 0 && a != NULL);
// Iterate the array from last element towards first element
size_t i = sz;
do
{
--i;
if (a[i] == n) return &a[i];
} while (i > 0);
return NULL;
}
int main(void)
{
int n = 3;
int ary[] = { 1,3,7,8,3,9 };
size_t elements = sizeof ary / sizeof ary[0];
int* p;
p = g(n, elements, ary); // or p = f(n, elements, ary);
if (p != NULL)
{
printf("Found at address %p - value %d\n", (void*)p, *p);
}
else
{
printf("Not found. The function returned %p\n", (void*)p);
}
return 0;
}
Working on the specified requirements in your question (i.e. a function that searches for the number and returns the address of its last occurrence, or NULL), the code below gives one way of fulfilling those. The comments included are intended to be self-explanatory.
#include <stdio.h>
// Note that an array, passed as an argument, is converted to a pointer (to the
// first element). We can change this in our function, because that pointer is
// passed BY VALUE (i.e. it's a copy), so it won't change the original
int* FindLast(int* arr, size_t length, int find)
{
int* answer = NULL; // The result pointer: set to NULL to start off with
for (size_t i = 0; i < length; ++i) { // Note the use of < rather than <=
if (*arr == find) {
answer = arr; // Found, so set our pointer to the ADDRESS of this element
// Note that, if multiple occurrences exist, the LAST one will be the answer
}
++arr; // Move on to the next element's address
}
return answer;
}
int main(void)
{
int num = 3; // Number to find
int ary[6] = { 1,3,7,8,3,9 }; // array to search
size_t arrlen = sizeof(ary) / sizeof(ary[0]); // Classic way to get length of an array
int* result = FindLast(ary, arrlen, num); // Call the function!
if (result == NULL) { // No match was found ...
printf("No match was found in the array!\n");
}
else {
printf("The address of the last match found is %p.\n", (void*)result); // Show the address
printf("The element at that address is: %d\n", *result); // Just for a verification/check!
}
return 0;
}
Lots of answers so far. All very good answers, too, so I won't repeat the same commentary about array bounds, etc.
I will, however, take a different approach and state, "I want to use a NULL-pointer" is a silly prerequisite for this task serving only to muddle and complicate a very simple problem. "I want to use ..." is chopping off your nose to spite your face.
The KISS principle is to "Keep It Simple, St....!!" Those who will read/modify your code will appreciate your efforts far more than admiring you for making wrong decisions that makes their day worse.
Arrays are easy to conceive of in terms of indexing to reach each element. If you want to train in the use of pointers and NULL pointers, I suggest you explore "linked lists" and/or "binary trees". Those data structures are founded on the utility of pointers.
int main( void ) {
const int n = 3, ary[] = { 1, 3, 7, 8, 3, 9 };
size_t sz = sizeof ary/sizeof ary[0];
// search for the LAST match by starting at the end, not the beginning.
while( sz-- )
if( ary[ sz ] == n ) {
printf( "ary[ %sz ] = %d\n", sz, n );
return 0;
}
puts( "not found" );
return 1; // failed to find it.
}
Consider that the array to be searched is many megabytes. To find the LAST match, it makes sense to start at the tail, not the head of the array.
Simple...

why is this recursive function doesn't return 'counter' value?

int rec(int k)
{
static int temp = 0, counter = 0;
if(!k) return counter;
if(k%2 == 0){
counter++;
temp = rec(k/2);
}
if(k%2){
counter++;
temp = rec(k-1);
}
}
This function is supposed to get a number k and check how many operations are needed to get k to 0 only by multiplying by 2 or by adding 1.
this function works fine but returns zero instead of the last value in the counter. I tried to debug it, and I saw the counter value increasing, but after getting to the value it is supposed to return, the function goes back to complete all the calls, and returns 0.
I fixed it by making counter global and returning nothing, but my questions are:
Is there a way to fix it as it is with the counter inside the function?
Why is this function returning zero at the end?
Here's a nice recursive function that doesn't declare any variables at all, it's purely recursive!
int get_steps_to_zero(int n)
{
// Deal with negative values
if (n < 0) n *= -1;
if (n == 0) {
// Base case: we have reached zero
return 0;
} else if (n % 2 == 0) {
// Recursive case 1: we can divide by 2
return 1 + get_steps_to_zero(n / 2);
} else {
// Recursive case 2: we can subtract by 1
return 1 + get_steps_to_zero(n - 1);
}
}
get_steps_to_zero(457);
> 13
Others have addressed the general recursion issue, yet there remains a ...
Corner case
"this function works fine" --> Not quite.
The algorithm is an infinite loop for rec(-1) as it attempts -1 --> -2 --> -1 --> -2 ...
A better approach would sometimes add 1.
if(k%2) {
counter++;
// temp = rec(k-1);
temp = rec(k + (k < 0 ? 1 : -1));
}
This also well handles non-2's complement issue.
I will try to show a new side to this discussion,
Recursion is like a chain,
This means that any function can receive a value from its child and return a value to its parent.
In your program you are trying to return a value from the last child to the first parent,
So your program must have all links in the chain receive and send value in order.
But in your code you return a value only in the last call, and all other calls do not return this value back.
Your stop condition is here:
if(!k) return counter;
Only the last call enters this scope, but other calls do not reach any return statement.
they get to here:
if(k%2 == 0){
counter++;
temp = rec(k/2);
}
if(k%2){
counter++;
temp = rec(k-1);
here there is no return statement.
your compiler will warning you like this:
"warning: control reaches end of non-void function".
but your program will compile so it return zero like all non-void functions without return.
So if you want this program to work make it like Randomblock1, or like this:
int rec(int k)
{
static int counter = 0;
if(!k){
return counter;
}
else if(k%2 == 0){
counter ++;
counter += rec(k/2);
}
else{
counter++;
counter += rec(k-1);
}
return counter;
}
But this program also do not work well, what is the reason?
The reason is because you used by static variable.
It's a little hard to explain, but you can't use this recursion with a static variable, so add a patch to this line:
static int counter = 0;
like this
int counter = 0;
I hope what I wrote will be understandable

Changing the value of elements in an array in C

My program crashes once it reaches this function in my code. I printed the arrays in the function that are going in and commented out everything else and
they are getting passed correctly. I can't figure out why it crashes here.
The arrays are numbers in ascending order.
Maybe my loops or if statements are not right? I think maybe I'm not correctly changing the value in the array at index "d" when it reaches attackerArmy[d] = '0'; //represent 0 as defeated ?
long int defeatedArmies(long int* attackerArmy, long int* defenderArmy, long int size){
long int i,j,defeated = 0,d;
for(i=0;i<size;i++){
for(j=0;j<size;j++){
//loop for the highest attack value that is lower than defender
if(attackerArmy[j] < defenderArmy[i])
d = j; // save the index where it is highest
if(attackerArmy[j] > defenderArmy[i])
//no point in checking unnecessary values
break;
}
attackerArmy[d] = '0'; //represent 0 as defeated
}
for(i=0;i<size;i++){
if(attackerArmy[i] == 0) //count how many defeated armies happened
defeated++;
}
return defeated;
}
Problem
if attackerArmy[j] >= defenderArmy[i] is true, d remains uninitialized causing the undefined behavior when you access its value in attackerArmy[d] = '0';.
Possible fix
Initialize d when you declare it.
ex:
long int d = -1L;
...
if(d != -1L) attackerArmy[d] = '0';

C: Recurrsive funcation calling with a negative sign

What's the result of a function being called with a negative sign in the front? Is it to turn the return value into negative?
int someFunction(int newBoard[9], int aValue) {
for(i = 0; i < 9; ++i) {
if(newBoard[i] == 0) {
newBoard[i] = player;
int thisScore = -someFunction(newBoard, aValue*-1);
// why this function being called with a negative sign? Is to turn the return value into negative?
}
}
return someValue
}
Yes that is correct, since someFunction returns an integer putting a '-' in front negates the result
e.g. int n = 1;
printf( "%d", -n );
...
-1
how this is used in your context is more difficult to guess since you do not provide much context in your question.
ya... Return value is stored as -ve value in variable thisScore..
Try this code...It may clear your doubt
#include<stdio.h>
int fun(void)
{
return 20;
}
main()
{
int a=- fun();
printf("%d\n",a);
}

Binary operation, need help

I am about to create a function for a program, this is part of a program and is meant to be a bitmap that holds controls of which memory address is free for use (this has nothing to do with this function to do). The bitmap is bit[64] which holds 8 x 64 bits, the function under is taking a parameter number that is the number of data blocks the function should occupy. In array data_blocks[] should the number to the data block that has bit value 0(free).
Execution of this program gives some strange outputs, and data_blocks[] gives values beyond the length of 512. Can someone please give me a hand? Thanks
#include <stdio.h>
#include <string.h>
void occupyDataBlocks(int number)
{
int ab = number;
char bit[512/8];
int bitNum = 0;
int count;
int data_blocks[ab];
int b;
for(bitNum = 0; bitNum < (sizeof(bit)/sizeof(char)); bitNum++) {
char x = bit[bitNum];
for(count = 0; x != 0; x >>= 1 ) {
if(!(x & 0)) {
data_blocks[count] = count;
}
if(count == number) {
break;
}
count++;
}
if(count == number) {
break;
}
}
if(count == number) {
int a;
for(a = 0; a < 5; a++) {
printf("%d\n", data_blocks[a]);
}
} else {
printf("Not enough data blocks\n");
}
}
int main(void)
{
occupyDataBlocks(3);
return 1;
}
k, where to start ...
1) "sizeof(char)" is most likely 1. So you have a 512-byte array, not a 64-byte array.
2) "bit" array is not initialized.
3) the assignment "char x = bit[bitNum]; " should occur inside the loop.
4) "strlen(bit)" does not do what you think it does. It interprets "bit" as a text string. You probably want do use "sizeof(bit)/sizeof(char)".
5) "(x & 0)" always evaluates to 0. What are you trying to do? If you're trying to test the bit, you want to do "!(x & 1)".
6) "int data_blocks[number]": does this even compile? You can't allocate a local array like that if its size is not known at compile time.
7) if(count == number) {
break;
}
only breaks you out of the inner loop. The outer loop continues on uninterrupted.
8) Do you really want to reset "count" to 0 every iteration of the outer loop? Do you want the code to find 3 free locations somewhere in the array, or 3 free locations in a single byte?

Resources