1

I have installed the gcc compiler from this sudo apt-get install build-essential command

and my program code is

 #include<stdio.h>

 main()
   {
      int *b;

      b = (int*)malloc(10*sizeof(int));  

      printf("b=%u\n\n",b);
      printf("b+1=%u\n\n",(b+1));
      printf("b+2=%u\n\n",(b+2));

      b[2]=4;
      printf("*(b+2)=%d\n\n",*(b+2));

  }

when i try to compile this program from cc -c program.c command then i get some error

enter image description here

  • 1
    You should not cast the return value of `malloc`. That's just a good way to hide errors. – Cody Gray - on strike Jan 15 '12 at 18:28
  • Your college video tutorial was made for an older version of gcc and should be updated :) Hmm wait a moment? Video tutorials for programming? What happened to reading and comprehending text? You're going to need it anyway to program well... – Torp Jan 15 '12 at 18:29
  • It's also strange to print out the value of a pointer. It's a completely meaningless value. Not sure what this demo is showing you, considering that you'll probably never do this again. The compiler is simply warning you that pointers are *not* `unsigned int`s. – Cody Gray - on strike Jan 15 '12 at 18:29
  • Also, there is no such thing as `void main()` in C. I'm really getting suspicious of that college you're attending and the study materials that they're providing! – Cody Gray - on strike Jan 15 '12 at 18:31
  • 1
    @CodyGray Casting the return value of `malloc` is a way to be compatible with C++. But it surely isn't necessary here. – pmr Jan 15 '12 at 18:32
  • @pmr: Yes, I'm aware of its necessity in C++, but the question is clearly tagged C here. It's also quite strange that you would ever use `malloc` in C++. – Cody Gray - on strike Jan 15 '12 at 18:35
  • cody gray,sir this is to know memory address –  Jan 15 '12 at 18:36
  • @CodyGray Of course. But some people consider it good practice. It really depends on what you want to do with the code and sometimes it's easier to compile C code with C++ code and then this kind of thing helps. – pmr Jan 15 '12 at 18:37
  • 4
    Why the h*** are you logged in as root user to compile your code? – knittl Jan 15 '12 at 18:50
  • knittl sir i haven't use any linux environment this is my first time. in turboc3 i haven't got problem/error like that –  Jan 15 '12 at 19:12

2 Answers2

6

You're missing #include <stdlib.h> (for malloc), and the format strings are wrong. Use %p to print pointers.

Also, you don't need to (and probably shouldn't) cast the return value of malloc (in C).

And the correct signature for main without parameters is:

int main(void)

Corrected code:

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

int main()
{
    int *b;

    b = (int*)malloc(10*sizeof(int));

    printf("b=%p\n\n",  (void*) b);
    printf("b+1=%p\n\n",(void*) (b+1));
    printf("b+2=%p\n\n",(void*) (b+2));

    b[2]=4;
    printf("*(b+2)=%d\n\n",*(b+2));

    return 0;
}
Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    I don't really believe that, sorry. I did the changes I described above to your code and it compiles without any warning. – Mat Jan 15 '12 at 18:34
  • Also you should cast the pointer to be printed to `void*` (in the unusual case `void*` and `int*` representations differ). – pmg Jan 15 '12 at 18:37
  • @pmg: I wonder how much code out there breaks horribly on such an implementation... – Mat Jan 15 '12 at 18:44
  • @mayank: I posted the full corrected code. It does not issue any warnings with GCC 4.5, 4, 4.7, and neither with clang nor icc. – Mat Jan 15 '12 at 18:45
0

I don't know why it worked in the video, it's probably using some strange non-standard compiler.

But your errors are because you are using int instead of unsigned int and you pass pointers to printf when it expects unsigned int.

Net4All
  • 21
  • 2