OT C Programmers: I need help

Discussion in 'OT Technology' started by suprropmp, Apr 11, 2005.

  1. suprropmp

    suprropmp New Member

    Joined:
    Nov 28, 2004
    Messages:
    5,326
    Likes Received:
    0
    Location:
    Los Angeles
    I really dont have much experience with C and I'm having trouble compiling this:

    Code:
    #include <stdio.h>
    
    main()
    {
    	int var1;
    	int *ptr = &var1;
    	int var2 = *(ptr + 10);
    	printf( var2 );
    }

    I get this error:
    try.c:8: warning: passing arg 1 of `printf' makes pointer from integer without a cast


    wtf does this mean?? how I just get it to output var2?
     
  2. veonake

    veonake OnT poster, OT lurker

    Joined:
    Apr 11, 2004
    Messages:
    4,235
    Likes Received:
    0
    Location:
    NYC
    Is: int var2 = *(ptr + 10); valid? Maybe I'll pull out my C book.

    Read aloud, that program would go something like "declare var1 as an integer type. declare ptr as the integer pointer to the address of var1. declare var 2 as an integer equal to ptr + 10...

    Ok, further thinking about it. Try var2 = *ptr + 10. Btw, you never set var1 equal to anything, you might want to do that for testing purposes, otherwise you'll just get whatever was randomly in that address location.

    Just realized I sold back my C book, sigh. Sorry I didn't help much. You should be able to figure out what you did wrong fairly quickly by reading up on pointers in your book. Read up on exactly what *ptr vs just ptr does. '&' of course is the address of that variable. * means value, no * I think means address. I could be wrong.
     
  3. Fei

    Fei Guest

    int var2 = *(ptr + 10);wtf? that doesn' tlook valid...check ur reference book
     
  4. Samdog

    Samdog mmmmmm, turbo.....

    Joined:
    Sep 10, 2002
    Messages:
    2,536
    Likes Received:
    0
    Location:
    Calgary
    to print an integer variable with printf try:

    printf("%d\n", var2);

    and this line: int var2 = *(ptr + 10);
    will most likely give you a seg fault at runtime
     
  5. kosstamogen

    kosstamogen New Member

    Joined:
    Aug 26, 2004
    Messages:
    386
    Likes Received:
    0
    Location:
    SoCal
    your printf syntax isn't quite rite like the guy above said, but why would you be trying to print out a bogus memory address?
    correct me if I'm wrong but, var2 points to a memory addr 10 spaces away from the original addr of var1. What are you trying to do?
     
  6. veonake

    veonake OnT poster, OT lurker

    Joined:
    Apr 11, 2004
    Messages:
    4,235
    Likes Received:
    0
    Location:
    NYC
    haha, I can't believe I missed that. That should have been obvious.
     

Share This Page