-2
(#) include < stdio.h >

int main () {

    int a = 10;
    int *p;

    p = &a;

    //Pointer arithmetic

    printf("Address p is %d\n", p);                       //error here
    printf("Value at address p is %d\n", *p);
    printf("Size of integer is %d bytes\n", sizeof(int)); //error  
    printf("Address p+1 is %d\n", p+1);                   //error as well
    printf("Value at address p+1 is %d\n", *(p+1));

    return 0;
}

Ok, as you see above, i am studying pointers under Objective-C. Note: This is my first time studying under Xcode version 8.3.3 using my Mac Sierra 10.12.5.

What I am experiencing in errors is that, and i quote: "Format specifies type 'int' but the argument has type 'int *' ".

Am I needing to download additional libraries or is their something i'm missing - to knock out this error?

Really appreciate the help and as well the Terminal code to help fix this error.

Please and thank you - and apologies, this is my first time using stack-over-flow question and site.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • 3
    The error message is clear. The type of `p` is `int*` but `%d` expects an `int`. – Jabberwocky Jun 14 '17 at 09:06
  • Sorry....I did not understand the question. Was the message unclear? I believe it would have also pointed out the line number containing the mistake...right? – Sourav Ghosh Jun 14 '17 at 09:08
  • 1
    You don't need more libraries, but a good C book. Read the chapter about types. And this is not specific to ObjC. – too honest for this site Jun 14 '17 at 09:11
  • @MichaelWalz So, how would i write in code to fix it? (Thank you for getting back to me - and would appreciate the use of you editing my code so i can see how it is to be written in C, for future use.) – SirLockInBottom Jun 14 '17 at 09:12
  • @Olaf What book would you recommend? – SirLockInBottom Jun 14 '17 at 09:13
  • @SouravGhosh If your talking about the error message: it reads as fallows "Format specifies type 'int' but the argument has type 'int *' ". If i am answering you correctly – SirLockInBottom Jun 14 '17 at 09:14
  • 1
    The one you find yourself. SO provides a list. Go, find it! – too honest for this site Jun 14 '17 at 09:14
  • 1
    Use `%p` to print pointers and don't forget to cast its argument to `void*`. `sizeof` returns `size_t` and the correct format specifier for `size_t` is `%zu`. – Spikatrix Jun 14 '17 at 09:15
  • Why not OP let find out? The message could not be more clear. – too honest for this site Jun 14 '17 at 09:16
  • @SirLockInBottom I don't mean to disrespect you, but a closer look would reveal something like `prog.c:12:27: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Werror=format=]`...so it says I have a problem in line 12, column 27....that should be enough indicator to pinpoint the erroneous instruction. – Sourav Ghosh Jun 14 '17 at 09:17
  • @Olaf We do have developer.apple website that i have been looking into, but using them still reaches a dead end. – SirLockInBottom Jun 14 '17 at 09:18
  • As I said: learn the basics! C is no language for trial&error learning. And websites or youtube are a bad idea for learning, too. – too honest for this site Jun 14 '17 at 09:21
  • @SouravGhosh Its alright dude, i am not disrespected at all really, haha. What i really want is an answer to my problem so i can continue coding with ease. And since your getting an error like that in Xcode (if your using it). Then, how would you rewrite, my code for it to function perfectly without any errors? – SirLockInBottom Jun 14 '17 at 09:21
  • @Olaf I do have a book called Data structures using c and C++ by Yedidya * Moshe* Aaron. 2nd edition. Will it provide anything - assuming you heard of these authors - i've been getting a lot of "Oh his or her, book is better" which makes it much more difficult to learn code since their are so many authors for the same subject. (thanks by the way) – SirLockInBottom Jun 14 '17 at 09:24
  • From the title, I'd throw it away. C and C++ are different languages, a book trying to cover both is already a major fail. It also seems to be directed to people knowing the languages already. I repeat: _you_ need the basics! And this is not a chatroom, so, that's all I have to say about this. – too honest for this site Jun 14 '17 at 09:30
  • @Olaf Thanks for that, i will try to find a better book to describe the Basics. – SirLockInBottom Jun 14 '17 at 09:33
  • @SirLockInBottom Welcome to Stack overflow. Suggestions for books usually start to get opinionated and the discussions usually go no where. So we avoid suggesting any books explicitly. Still, you can look at [this](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) question which mentions some really good books. – Ajay Brahmakshatriya Jun 14 '17 at 09:36
  • @SirLockInBottom Coming to your problem, what you have done is you are trying to print an address but in the printf format specifier you have written %d. A %d is used when you want to print an integer. An integer is not a pointer and is also not convertible as such. If you actually want to print the pointer you need to change %d to %p in the first printf (and also the one with p+1). Finally also cast p to `void*` as `(void*) p`. – Ajay Brahmakshatriya Jun 14 '17 at 09:38
  • @SirLockInBottom Regarding what @SouravGhosh was saying, that is a very important point. You need to learn how to interpret the error messages. I am not aware of Xcode but usually all compilers give a very descriptive error message which should point you to the most probable error site. Usually the error message is also accompanied with a line number. If your compiler doesn't have such error messages (or if the IDE is hiding them) I suggest use some other compiler. I believe you are using a Mac for development. So `clang` should be a good choice. – Ajay Brahmakshatriya Jun 14 '17 at 09:41
  • @SirLockInBottom AFAIK Xcode must be configured to use `clang` as a compiler. But you can manually invoke the compiler from command line (with appropriate flags so it shows you all errors and warnings). – Ajay Brahmakshatriya Jun 14 '17 at 09:42
  • @AjayBrahmakshatriya Appreciate the help, the %p manage to turn it to Hex form - which is good enough to understand the address. I will use clang as a new IDE rather than Xcode and will see how that goes. I don't really know how to manually do the invocation for flags. I didn't know you could do that. – SirLockInBottom Jun 14 '17 at 09:52
  • @SirLockInBottom hex is usually a good notation to read addresses because in decimal the numbers tend to get very large and all the "good" sizes like 16 bytes, 1024 bytes, 4096 bytes look better in hex. `clang` is not an IDE but a compiler. An IDE uses a compiler internally. You can invoke a compiler manually from a terminal. The flags which I mentioned are usually passed along with the file name to the compiler. You can type `clang --help` to see a list of all flags. The flag `-Wall` should be enough for you and will display all warnings. – Ajay Brahmakshatriya Jun 14 '17 at 09:56
  • @AjayBrahmakshatriya Umm, quick question what if the Zip file of Clang ends up opening another zip file on-top of another zip file? Is that suppose to happen? – SirLockInBottom Jun 14 '17 at 09:57
  • @SirLockInBottom I am guessing you are trying to install clang. I have a feeling your machine must already have it installed. In a terminal try running `cc --version` and check if it prints anything which says `clang ...`. If yes, you can directly start using `cc` as a compiler. You don't need a zip file. – Ajay Brahmakshatriya Jun 14 '17 at 10:00
  • @AjayBrahmakshatriya Ummm. . .We have a problem, using the terminal dose not have -Wall on it. So whats the next step? – SirLockInBottom Jun 14 '17 at 10:00
  • @SirLockInBottom What was the result of `cc --version` in the terminal? – Ajay Brahmakshatriya Jun 14 '17 at 10:01
  • @AjayBrahmakshatriya Here is what i got on the cc --version: **cc --version Apple LLVM version 8.1.0 (clang-802.0.42) Target: x86_64-apple-darwin16.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin** – SirLockInBottom Jun 14 '17 at 10:02
  • @SirLockInBottom Great! You already have clang installed. You can start using it directly. Type in `cc --help` and it will have all information on how to use the compiler. – Ajay Brahmakshatriya Jun 14 '17 at 10:03
  • @AjayBrahmakshatriya So i will be using the terminal itself to write in C, correct? – SirLockInBottom Jun 14 '17 at 10:05
  • @SirLockInBottom You can also look at [this](https://stackoverflow.com/questions/32337643/how-to-run-c-program-on-mac-os-x-using-terminal) for help on how to compile C code from the terminal and run it. You can write the code anywhere. In a normal text editor, Xcode anything. Just save it as a .c file and use the terminal to compile and run it. For beginner users it is advisable to use a text editor/IDE. You can continue using Xcode to write the code. – Ajay Brahmakshatriya Jun 14 '17 at 10:05
  • @AjayBrahmakshatriya I'm actually getting more confused, so after knowing that my version of C is correct, and that the Terminal itself for getting Xcode downloaded; i can now write code on the Terminal? (I'm Dyslexic so i apologies for my . . . mental shortcomings.) – SirLockInBottom Jun 14 '17 at 10:12
  • @SirLockInBottom Not a problem at all. So you see you need a text editor to write the code. You can write from the terminal but it might be a difficult to use. Given that you are already familiar with Xcode, use that to write the code. Then save the file as a .c file. You can then use the terminal to compile and run the code. – Ajay Brahmakshatriya Jun 14 '17 at 10:17
  • @SirLockInBottom if terminal is too confusing, you can continue to use Xcode as before(for everything), just pay more attention to the Warnings and errors it displays. – Ajay Brahmakshatriya Jun 14 '17 at 10:18
  • `(#) include < stdio.h >` shouldn't compile. Change to `#include `. – Lundin Jun 14 '17 at 11:08

1 Answers1

0
  • %d is a format specifier. %d in particular is used to print arguments of type int.
  • p is not an int, it is a pointer to int, type int*.
  • To print pointers, use the %p format specifier.
  • *p is however of type int, since you de-referenced the pointer. For such arguments you should use %d.

As a side note, the result of sizeof isn't int but another integer type called size_t. To print this one, you need to use %zu.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Overall, don't spend too much time learning about the obscure stdio.h library. It has lots of complexity and is also generally dangerous. It is rarely used in professional production code. – Lundin Jun 14 '17 at 11:11