0

I just started learning, I didn't understand the book, so I asked for advice. I am a beginner and don't have a good English. Function: Combine two two-digit positive integers A and B to form an integer in C Middle. The method of merging is: the ten digits and single digits of the A number are placed on the thousand and ten digits of the C number, and the ten and single digits of the B number are placed on the single and hundred digits of the C number. For example: when a=45, b=12. After calling this function, c=4251. Here is my code

#include <stdio.h> 
  
void fun(int a, int b, long *c);
 
int main()   
{ 
  int a,b;
  long c; 
  int state = 1;
  printf("Enter a: ");
  printf("(q to quit)");
 
  while( scanf("%d",&a)==state)
  {
    printf("Enter b: ");
    printf("(q to quit)");
    while( scanf("%d",&b)==state)
       {
          fun(a, b, &c);     
          printf("The result is: %ld\n", c);
 
       }
  }
  return 0;
}   
 
  void fun(int a, int b, long *c)     
{
  /**********Program**********/
    *c = 100*(a%100)+b%100;
  /**********  End  **********/
}

I tried removing the * and found that the result was 16. It is wrong but not know why

Levis
  • 23
  • 4
  • 6
    Imagine that you hired a house-painter to paint your house. You give him a piece of paper (`c`) with your home address on it, and say "please paint this house green". If he does the right thing (`*c = [...]`), he'll drive to your house and paint the house green. If he does the wrong thing (`c = [...]`) he'll paint the piece of paper green, and when you next go to look at your house, you will find it hasn't been painted at all. – Jeremy Friesner Feb 04 '23 at 05:51
  • 2
    Btw the correct way to call `fun()` in your example would be `fun(a, b, &c);` ... without the `&` you are trying to pass a `long` to an argument that requires a `long *`, and that shouldn't compile. – Jeremy Friesner Feb 04 '23 at 06:00
  • Print out the address of `c` in `main()`, and then do the same in `fun()`. See if they both point to the same thing. Then pass a pointer to `c` as an argument to `fun()`, and print out the address contained in `c`. See if the address of `c` in `main()` and the address contained in `c` `fun()` are identical. – Harith Feb 04 '23 at 06:10
  • 1
    DO NOT suggest to the user to enter 'q' to quit when `scanf()` is expecting to see an integer value... Entering text will send the program off into space... – Fe2O3 Feb 04 '23 at 06:11
  • In the book C prime plus, I saw a similar expression like this, so I tried using it – Levis Feb 04 '23 at 06:58
  • 1
    Please do not edit the code after posting. It invalidates the existing comments and answers.. – Harith Feb 04 '23 at 08:27
  • A couple of links that provide basic discussions of pointers that may help. [Difference between char *pp and (char*) p?](https://stackoverflow.com/a/60519053/3422102) and [Pointer to pointer of structs indexing out of bounds(?)...](https://stackoverflow.com/a/60639540/3422102) (ignore the titles, the answers discuss pointer basics) There is nothing magic about pointers, but when new to C, it can take looking at them from a few different angles before the light-bulb winks on. (just a normal variable that holds the memory address for something else, e.g. it points to the memory location) – David C. Rankin Feb 04 '23 at 10:10
  • Sorry, I don't know much about this, but I saw people say I had a mistake and then correct it – Levis Feb 04 '23 at 11:09

1 Answers1

3

The parameter long *c means c is an address of a variable (in this case the variable in main() is called c and you need to call the function like this fun(a, b, &c)).

When you want to update the value stored at that address c the syntax is *c = .... If you do c = ... you are updating the address of the variable which has no external effect.

Alternatively, you could change your function to return a value and the call would then look like this:

   c = fun(a, b);

and the function would be:

int fun(int a, int b) {
    return 100*(a%100)+b%100;
}
Allan Wind
  • 23,068
  • 5
  • 28
  • 38