What does this declaration mean..*ptr="SomeChars"[var1%var2] ?

This is a discussion on What does this declaration mean..*ptr="SomeChars"[var1%var2] ? within the C Programming forums, part of the General Programming Boards category; Hi, I came across such an expresiion from a C book..whose explanation was not there. *ptr="SomeChars"[var1%var2] ? Here ptr is ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    New York
    Posts
    24

    Angry What does this declaration mean..*ptr="SomeChars"[var1%var2] ?

    Hi,

    I came across such an expresiion from a C book..whose explanation was not there.

    *ptr="SomeChars"[var1%var2] ?

    Here ptr is pointer to char and var1 and var2 are two int varible.

    I am confused about the syntax and its meaning.

    Pls, explain a bit.

    Thanks,
    --A
    Last edited by Alexpo; 06-10-2008 at 04:27 AM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Code:
    *ptr="SomeChars"[var1%var2]
    Breaking it into several steps:

    Code:
    int index = var1 % var2;
    const char* mystr = "SomeChars";
    char c = MyStr[index];
    *ptr = c;
    First line: Take the remainder of dividing var1 and var2 and store in index.
    Second line: Define string literal mystr with the contents "SomeChars".
    Third line: Define the variable c and put the char stored at index inside.
    Fourth line: Assign the char stored in c at the position pointed by ptr.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's a weird expression indeed. Is this book called "Obscure and strange expressions in C" or some such?

    The way it works is that it assigns whatever ptr points at with the character at index as calculated by var1 % var2. The fact that the string is a constant is no different from if you did:
    Code:
    const char *str = "SomeChars";
    ...
    *ptr = str[var1%var2];
    A modern compiler will most likely produce exactly the same code for both of these, but the code I posted is a bit less "smart-arse".

    Edit: Elysia beat me to it...

    --
    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
    Mar 2008
    Location
    New York
    Posts
    24
    Thanks Elysia and Mats.

    Its realy a weired expression and only ok in books teaching different (and complex)way of doing things and should be avoided in reallife coding.

    Regards,
    --A

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Avoided? Not really.
    The book is correct is feeding you complex samples.
    C is a complex language and everyone has different styles of writing it.
    So it's trying to teach you to understand such code should you encounter it (and maybe give you an insight on what's possible, too).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is not something I would write, but at the same time, I could understand that code.

    Different people and groups of people have different opinions on what is right and wrong in coding (as well as in other things in life, of course).

    Writing code that is clear and easy to understand is always my goal - but of course, if you ask someone of the legal profession, writing clear and understandable English will not have the same meaning as a school teacher teaching reading and writing to 5-7 year olds.

    --
    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.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    Not that I advocate code like this, but you will also find;
    Code:
    *ptr= (var1%var2)["SomeChars"] ;
    has the same result. One of the marvels of pointer and array syntax in C.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Depends how the book approaches the issue.

    If they're trying to make themselves look "smart" and indicate that this is somehow common in production code (which it isn't), then it's a crap book.

    But if it's all "tongue in cheek", and you know it is, then it's just a harmess fun fact
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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