I tried compiling this example on both Mac (10.10.5) and Linux (RedHat 5.5):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str;
/* Initial memory allocation */
str = (char *) malloc(15);
strcpy(str, "tutorialspoint");
printf("String = %s, Address = %s\n", str, str);
/* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %s\n", str, str);
free(str);
return(0);
}
I receive no errors when compiling and running the executable on Linux (gcc) and receive the following output:
String = tutorialspoint, Address = 294019088
String = tutorialspoint.com, Address = 294019088
But I receive these errors when compiling on Mac (gcc):
testmemalloc.c:12:48: warning: format specifies type 'unsigned int' but the argument has type 'char *' [-Wformat]
printf("String = %s, Address = %u\n", str, str);
~~ ^~~
%s
testmemalloc.c:17:48: warning: format specifies type 'unsigned int' but the argument has type 'char *' [-Wformat]
printf("String = %s, Address = %u\n", str, str);
~~ ^~~
%s
2 warnings generated.
but the executable outputs a number as expected:
String = tutorialspoint, Address = 1631603760
String = tutorialspoint.com, Address = 1631603760
Now, please forgive the stupid question, I'm new to C (and not a programmer). Even though the executable runs and outputs a number as expected, can someone please explain the difference here due to OS and the potential impact, if any?