Thread: #define

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    #define

    If you use #define for the first example this works fine.

    First Example:
    Code:
    #define Number number
    
    int Number = 0;

    If you use #define for this example it doesn´t compile.

    Code:
    #define Number[1] number[1]
    
    std::vector<int> Number(500);
    Why does this not work ?

    I dont know if it is because it is [] etc...
    Is it possible to #define things like this or point to them in any way.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    AFAIK a define replaces each instance of one pattern with another in your prog. So here:
    Code:
    std::vector<int> Number(500);
    nothing will be replaced as it does not match 'Number[1]'

    Also why do you want to do this anyway?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I dont know if it is because it is [] etc...
    Is it possible to #define things like this or point to them in any way.
    hmm... I have never seen such a macro before. MSVC8 reports "error C2008: '[' : unexpected in macro definition". The MinGW port of g++ 3.4.5 reports "warning: ISO C requires whitespace after the macro name". The Comeau online compiler does not complain at all.

    Anyway, macros considered harmful in C++, so you might as well avoid them if you can.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    >> nothing will be replaced as it does not match 'Number[1]'
    The thing is that you could for example write
    #define Something something

    Even that you haven&#180;t anything declared or named "Something" in the code.
    For example Number[1] isn&#180;t declared as such but yes it seemed that it was needed
    blank spaces after the macro name.

    Why I want to do this is because my code will generete hundreds of examples like this:

    Number[d+1]
    Number[d+2]
    etc...

    I will myself write code manually all the time in this program, so instead of writing Number[d+2] hundreds of times it would be simplier to just write Number[2] wich will meen Number[d+2].

    If it doesn&#180;t work with a macro like this. Is there any other way to do this. Use pointers or any other technique.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Coding View Post

    If it doesn´t work with a macro like this. Is there any other way to do this. Use pointers or any other technique.
    Yes - use pointers or iterators
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As in you want to map Number[n] to number[d+n]? One way would be to use a function, or even a function object. It depends on what exactly are you trying to use this in.

    EDIT:
    On second thought, if you use a function you need to pass d in anyway (or use a global variable, which can be a Bad Thing), and a function object might be unnecessarily complicated. If that is the case, then a pointer/iterator would be better.
    Last edited by laserlight; 02-21-2008 at 09:52 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I can take a simple example with a code:
    I will read a textfile line by line, these lines consist of these numbers:

    1
    2
    3

    Code:
    std::vector<int> Number(500);
    
    int d = 500;
    int NumberX = 0;
    
    while ( getline(File, NumberX, '\n') )
    {
          d = (d - 1);
    
        
          File.get();
    
          Number[d] = NumberX;    // For the first line NumberX[d] will have value 1
     
    }
    So if you are reading this textfile the Number[d] for the first line will have value: 1
    Now, for the second line:

    Number[d] will have value: 2
    and
    Number[d+1] will have value 1

    Because I did a count down with d = (d - 1);

    So when reading files I will have to refer to previous values like this, so instead of
    writing in this example Number[d+1] I will just write Number[1] and this will meen the same thing.
    Last edited by Coding; 02-21-2008 at 10:07 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not to mention doing that would be extremely confusing. First you would mistake it for real code and not some substituted code and secondly, you would have to go find that macro to understand it and third, it would affect all such variables with that exact name, which is very bad, unless you undefine it.
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I do not understand what you are trying to do, and the code example does not really help since it is incorrect.

    Why not provide a minimal, compilable example of what you are trying to do? You do not need to start with d = 500, just d = 5 will do and then you show why you find it tedious to write the code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I will give an exact example of how this is done and why I want to do this.

    The code look like this and I will read 5 lines from a .txt file that look like this:

    1,1
    1,2
    1,3
    1,4
    1,5

    The code to read these look like this:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <string>  
    #include <vector>
    
    using namespace std;
    int main () 
    { 
    	std::vector<int> Number(5);
    	
    	std::string Number1;
    	int d = 6;						// Put d to its startvalue = 5;
    	int Number2 = 0;
    	int Number3 = 0;
    
    	ifstream File("File1.txt");
    					
    	while	(    getline(File, Number1, ',')   )           		
    	{
    		
    		d = (d - 1);				// CountDown, First Value is (6-1) = 5
    				 
    		File >> Number2;
    		File.get();
    
    		Number[d] = Number2;		// For the first line red in the file: Number[d] will have value 1
    
    
    			if (Number[d+1] == 1)
    			{
    				Number3 = 1;
    			}
    
    	}
    
    return 0;
    }
    So what am I doing here ? While reading the file I am doing a countdown for int d = 6 so for the first line this will have the number: 5
    As seen now below this I am giving Number[d] the value of Number2 wich is the first line in the textfile: 1

    For the next line that is red in the file d wil have a value of 4 as it is a countdown so Number[d] will have the value of 2 this time.

    This moment Number[d] will have the value of 2 and Number[d+1] will have the value of 1. yes.

    Below this I am doing a ifstatement that says if (Number[d+1] == 1) { Number3 = 1};
    This is True while reading the second line, yes.

    So now you probably are asking why I want to find a way to write Number[1] instead of Number[d+1] as this works fine ?

    There is a huge reason why. This is because I will write 100:s of pages of code that will exist of this example. This was a very easy example.
    So writing Number[1] instead of Number[d+1] is easier and better for my purpose. So to find a way to do that would be nice how it could be done.
    With pointers or other approches.
    Hope this understands better what I am trying to do.
    Last edited by Coding; 02-21-2008 at 11:06 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So what am I doing here ? While reading the file I am doing a countdown for int d = 6 so for the first line this will have the number: 5
    As seen now below this I am giving Number[d] the value of Number2 with is the first line in the textfile: 1
    hmm... I am still a little unsure of what it is all about. For example, your comment says "For the first line red in the file: Number[d] will have value 1", but Number2 does not have to be 1. Furthermore, by fixing the vector at size 5 and not checking the number of elements in the loop you risk an out of range problem if the input is not as expected.

    There is a huge reason why. This is because I will write 100:s of pages of code that will exist of this example. This was a very easy example.
    So writing Number[1] instead of Number[d+1] is easier and better for my purpose.
    There is almost no difference between Number[1] and Number[d+1] in terms of how much you have to type. Besides, saving keystrokes is not a valid reason for a design. If you want to create an abstraction (and you probably should), you should abstract away the entire example into functions and/or classes to improve readability and reusability, not just designing for a cosmetic change of Number[d+1] to Number[1] (or something along similiar, pessimistic lines).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I agree with laserlight. Just typing Number[1] will obfuscate the code and will confuse readers.
    And your code is flawed, as well: you initialize d to 6, count down to 5 inside the loop, access element 5 which does not exist. Further, you try to access d + 1, 6, which doesn't exist either.
    Remember that indexes start from 0, so 0 to 4 exists, but not 5 or 6.
    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.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes I knew that it began from 0 to 4, this was just a fast writing mistake. This will confuse you and me if we look at this code perheps but the thing is that I am trying to redefine some of the syntax so other people that dont know C++ syntax can write own code that they already know about. This code is that I will redefine. They dont know C++ so for these people it will be confusing to write for example then Number[d+1] instead of Number[1] as they are used to and many other examples. This is the reason why I try to do it. I understand it seems a little bit confusing ofcourse.
    Last edited by Coding; 02-21-2008 at 11:33 AM.

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This will confuse you and me if we look at this code perheps but the thing is that I am trying to redefine some of the syntax so other people that dont know C++ syntax can write own code that they already know about. This code is that I will redefine. They dont know C++ so for these people it will be confusing to write for example then Number[d+1] instead of Number[1] as they are used to and many other examples.
    I suggest you stop now. If they don't know C++ syntax, but can program in another language then a) why can't they continue programming in a language that they know, 2) why can't they learn new syntax - basic syntax is really easy to learn if you know how to program in general?

    I don't think this is a little bit confusing - this is very confusing and changing the language syntax is clearly a misuse of #define.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think I missed that part. It's clearly a bad thing, since it's must clearer to write Number[d+1] than Number[1], since obviously Number[d+1] is not Number[1]. It's an entirely different index. It would only serve to confuse.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Why?!?
    By p3rry in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 12:52 PM
  3. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  5. float toolbar!
    By c-- in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2003, 09:44 AM