0

am trying any example from here https://www.tutorialspoint.com/cprogramming/c_pointers.htm and they compile and work in codeblock but in cs50 ide its always:

 #include <stdio.h>

int main () {

   int  var1;
   char var2[10];

   printf("Address of var1 variable: %x\n", &var1  );
   printf("Address of var2 variable: %x\n", &var2  );

   return 0;
}

error: format specifies type 'unsigned int' but the argument has type 'int *' [-Werror,-Wformat] printf("Address of var1 variable: %x\n", &var1 );

and similar errors. i googled for 2h, trying to figure it out but not much found out.

also, we always used way of storing input by using get_string(). am extremely curious how would working code of this example would look like with working scanf implemented instead of get_string(cause i dont know how to do it):

#include <cs50.h>
#include <stdio.h>
#include <string.h>

int main(void)
{


//get line of txt
char *s = get_string();
    if (s == NULL)
    {
         return 1;
    }

//print string, once character per line
    for (int i = 0, n = strlen(s); i < n; i++)
     {
         printf("%c\n", *(s+i));
     }
}

thanks in advance

1

Printing out the address of a variable

"%x" does indeed require that the variable you are trying to print is an unsigned int, but &var1 and &var2 are both pointers (int* and char (*)[10] respectively). In order to print pointers you must use the "%p" format specifier, so the calls to printf should look like this:

printf("Address of var1 variable: %p\n", &var1);

See the printf man page and/or the CS50 reference page (if you uncheck the "Less Comfortable" box) for more information.

Reading a string from stdin

Taking string input with scanf would look like this:

char str[10]; // char *str = malloc(10 * sizeof (char)) would also work
scanf("%s", str)

However, you should almost never read in a string with scanf for two reasons:

  1. It will only read until it encounters the first whitespace (space, tab, newline etc.) by default which may or may not be the desired behavior.

  2. What happens if the user inputs a string longer than 9 characters (not including the null byte) in the above example? scanf has no idea about how big str is so it would write past the end of the array which is undefined behavior. You can add a length specifier e.g., scanf("%9s", str) but really you are probably better off using a different function all together.

fgets is probably what you want if you know the size of the string beforehand as it reads an entire line of text in by default and requires you to specify the size of the buffer. You'd call it like this:

char str[10];
fgets(str, 10, stdin);

If you don't know the size of the string beforehand, it's a bit more complicated (which is why get_string exists in the first place). If you want to see how get_string works, you can head over to the the GitHub repo for the CS50 library: here.

1
  • thank you so much for answer. i never come across %p before, i cant see it being mentioned on man pages either. it works now! theres so much going under the hood of get_string()! i will learn the right way, like you described, of getting input but the way you used scanf is same as i tried and its not working. its a bit annoying because in lecture it was mentioned that we can use scanf instead of get_string but it seems to not be the case. its really weird when u try something that should be easy, and just dont work, and you get stuck :D thank you so much for help! i can practice pointers now
    – Belutak
    Apr 16 '17 at 10:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .