Dumb Question for C Newbie

This is a discussion on Dumb Question for C Newbie within the C Programming forums, part of the General Programming Boards category; I am taking a C application and re-writing it in C# but am having a problem understanding the C syntax. ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    Dumb Question for C Newbie

    I am taking a C application and re-writing it in C# but am having a problem understanding the C syntax.

    I have these three lines of code:
    Code:
    double ti(unsigned char *data)
    {
    int address=0x26;
    unsigned char *tempdata = data + address;
    return tempdata
    }
    What is the third line doing? It seems odd to be 'adding' an integer to a char. What would you expect the value*tempdata to hold and what would be returned?
    Last edited by Salem; 08-13-2008 at 12:44 PM. Reason: Added code tags - learn to use them yourself.

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    The third line returns the address of *tempdata. Value of *tempdata is data + address as indicated on the second line. Btw, the third line should have a semicolon at the end of the line.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is not adding an integer to a char, it's adding an integer to a char pointer - that means, it is calculating an address at an offset from the base pointer (data).

    I expect the compiler to be fairly unhappy about returning a unsigned char pointer as a double value, so I don't think it will compile (at the very least it should give a warning).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    Smile Thanks

    Many thanks guys - that explains everthing in language I can undestand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  3. Dumb I/O Performance Question
    By BENCHMARKMAN in forum C Programming
    Replies: 18
    Last Post: 02-24-2008, 05:26 AM
  4. dumb question...
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-28-2002, 10:35 AM
  5. Yet another dumb newbie question!
    By RobR in forum C++ Programming
    Replies: 8
    Last Post: 02-24-2002, 06:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21