Novice C programmer needs help with pointers (I think)

I am a beginner programmer and I am learning my first language, C.

I mainly study Deitel and Deitel C How to Program books, but also using exemplary tasks and things from the university, however I am stuck on one.

I have a very general understanding of pointers - adding and before a variable causes it to print an address and * uses a pointer to use the value stored on that address or such.

The piece of code that I wrote is designed to calculate the largest (largest?) Common denominator of two numbers and is not really needed or does not contain pointers at all. It uses two functions, and the logic is correct, because it displays the correct answer on the screen, if I do it from the second function, instead of returning it to the main one. This is where the problem lies.

When the second function returns the response value, for some reason it returns what I can only consider as a pointer. I have no idea why he is doing this. I could work with this and convert it to look up a value - however, it seems to be a pointer to a local second function and is written on top. Nothing on the Internet that I could find or in my book gave me an idea of ​​how to solve the problem.

Thank you if you read this far. I mixed up too much.

Here is my code and output. Any help or pointers (sorry for the pun) would be greatly appreciated. I know that I could just print it in the second function, but I would rather know how and why it does not return a value as I would like.

the code

#include <stdio.h>
int greatestCD (int num1, int num2);

int main(void)
{
    int a=0, b=0;
    int result;
    printf("Please enter two numbers to calculate the greatest common denominator from\n");
    scanf("%d%d", &a, &b);
    result = greatestCD (a,b);
    printf("Using the correct in main way:\nThe greatest common denominator of %d and %d is %d\n",a,b, result);
}

int greatestCD (int num1 ,int num2)
{ 

    if (num2==0){
        printf("Using the cheaty in gcd function way:\nThe greatest common denominator is %d\n",num1);
        return num1;
    } else {
        greatestCD(num2,(num1%num2));
    }    
}

Exit (using 12 and 15 - the answer should be 3)

C:\Users\Sam\Documents\C programs>gcd
Please enter two numbers to calculate the greatest common denominator from
12
15
Using the cheaty in gcd function way:
The greatest common denominator is 3
Using the correct in main way:
The greatest common denominator of 12 and 15 is 2293524

Such a simple solution from frankodwyer. These are tiny things that I either can’t understand or don’t know. What came back was not a pointer and was just trash?

Thanks a million.

+3
source share
5 answers

The last line of the maximumCD () function

missing 'return' statement
greatestCD(num2,(num1%num2));

To do this

return greatestCD(num2,(num1%num2));

( , ... , )

edit: , ... gcc, -Wall . , , . ( , "", .)

+10

C, , :

  • , malloc() free(). .

  • Deitel Deitel Kernighan Ritchie. , - .

  • , . MacOS Linux, gcc -Wall -Werror -O .

  • C valgrind. Valgrind - , catch , . !

!

+4

, 2293524 - . ?

+2

. Herb Schildt , , , http://www.cs.technion.ac.il/users/yechiel/CS/BadBooksC+C++.html. , ++ Deitel Deitel, . C. ( , ), - (, C).

+2

, Linux-. gcc?

/dev/shm $ gcc x.c
/dev/shm $ ./a.out
Please enter two numbers to calculate the greatest common denominator from
12
15
Using the cheaty in gcd function way:
The greatest common denominator is 3
Using the correct in main way:
The greatest common denominator of 12 and 15 is 3
/dev/shm $ gcc --version
gcc (Debian 4.3.2-1) 4.3.2
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0

All Articles