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

void south_east(int *lat, int *lon){                       // lat and lon are the pointer variables which take addresses as input  , int *lat = &latitude
  *lat = *lat - 1 ;                                       // Using the * operator to update the values at the address.
  *lon = *lon + 1;
}

int main(int argc, char const *argv[]) {
  int latitude;
  int longitude;
  scanf("Enter Latitude and Longitude %i\n %i\n", latitude, longitude);
  south_east(&latitude,&longitude);                                        // We pass the addresses so that the function above can easily modify them
  printf("Move to [%i, %i]\n", latitude, longitude );
  return 0;
}
ships.c: In function 'main':
ships.c:14:40: warning: format '%i' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat=]
   scanf("Enter Latitude and Longitude %i\n %i\n", latitude, longitude);
                                        ^
ships.c:14:45: warning: format '%i' expects argument of type 'int *', but argument 3 has type 'int' [-Wformat=]
   scanf("Enter Latitude and Longitude %i\n %i\n", latitude, longitude);
                                             ^
ships.c:14:3: warning: 'longitude' is used uninitialized in this function [-Wuninitialized]
   scanf("Enter Latitude and Longitude %i\n %i\n", latitude, longitude);
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ships.c:14:3: warning: 'latitude' is used uninitialized in this function [-Wuninitialized]

What's wrong with this? When I hardcode the values of latitude and Longitude, I get the desired output but when I use scanf() to get inputs, it gives me warinings!

5
  • scanf needs pointers to variables to write the result into them Commented Nov 20, 2020 at 19:16
  • You are not asking about implementing scanf. You're just asking how to use it. Commented Nov 20, 2020 at 19:22
  • @Raildex Could you tell me what I need to add? Commented Nov 20, 2020 at 19:22
  • You need to add compiler flags that will generate warning that tell you exactly what is wrong with your code. Commented Nov 20, 2020 at 19:23
  • Just as a side note: It is unsafe to use scanf without checking the return value. See this page for further information: A beginners' guide away from scanf() Commented Nov 20, 2020 at 19:39

1 Answer 1

1

Change this line to send addresses, not values. Also - in scanf define only what you want to read, not the message

printf("Enter Latitude and Longitude ");
scanf("%i %i", &latitude, &longitude);
Sign up to request clarification or add additional context in comments.

1 Comment

when I enter the values as 21 and 12 I get this as output 'Move to [4194431, 3805185]' . I think it is outputing the address!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.