Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Related to #Define (http://www.go4expert.com/forums/related-to-define-t16721/)

abhushan_s 30Mar2009 12:33

Related to #Define
 
Hi all,

I have a simple question, which iam probably not able to figure out conceptionally. :crazy:

It goes like this

void test( int * variable)
{
#define one variable [0]
#define two variable [1]
}

Here the variable is the pointer to the first address of the passed value from some other function or main.
Now the question is that "will the scope of the two variables" be only inside the function "Test()" or will it be outside the function also? :wacky:

alwaysLearning 30Mar2009 16:42

Re: Related to #Define
 
Hi,

So far i know you can not declare or use a "define" macro in a function like this.
But as per as scope of variable concern, here "variable" has declared as local variable, so no mater from where the value has been passed, you can not use the "varaible" out side this function. But as it is a pointer type variable, that means, if you change any value to the address to which this pointer is pointing, it will effect that int, where that int has been declared. ( ex varaible[0] = 10 it will change the original arrays[0] which u have passed to this funtion).

If all these are confusing.. sorry for that, but if u have any question dont hesitate to ask.

xpi0t0s 31Mar2009 03:15

Re: Related to #Define
 
#-lines are processed lexically with regard only to line numbers, and you can use them anywhere it makes sense to do so.

So the following is perfectly fine:
Code:

int variable[2];

void test(int *variable) // rename variable to anything you like, it has no effect
{
#define one variable[0]
#define two variable[1]
}

int main()
{
variable[0]=5;
printf("%d\n",one);
return 0;
}

The function test() does absolutely nothing and you don't need to call it to set "one" and "two". The above should print 5 (haven't done a run test).

The thing to remember about #defines is that they define symbols within the file from the line where they are defined to (a) the end of the file or (b) the line where they are #undef'd.

alwaysLearning 31Mar2009 04:14

Re: Related to #Define
 
Hmm.. so we can declare define in a function like this!
But still i would suggest its not a good practice to use define to define a variable like this within a function.

Code:


int VARIABLE[2];

void test(int *variable) // rename variable to anything you like, it has no effect
{
#define one variable[0]
#define two variable[1]
}

int main()
{
VARIABLE[0]=5;
printf("%d\n",one);
return 0;
}

In the above example, still i think its not a very good thing to use as, the use of "one" in the main actually using the golbal "VARIABLE", not the local "variable". If you change the name of the global variable to Upper case variable or someother thing, it will get a compiler error. because one actually representing the code "varaible[0]" this variable is the local one declared in the function.

abhushan_s 31Mar2009 12:42

Re: Related to #Define
 
Hey Thank you for the reply! Appreciated! :)

But actually this was a question asked to me, it was that whether the scope of the variables would be local or global, what i wanted to ask is that, even i thght in the beginning is that this is not the right way to declare the Macro, beacuse whenever the varible is encountered it would be changing the value of it to the one declared in the definintion of the Macro, but then i got to know that the method was not efficient to use but not wrong either, but what makes me confused is that if we are using a Macro then why is it required to make is Local to a function alone? because if it is a Macro then it will be used only in the place where it is needed so it is unnecessary to use it like this!

I did not find this correct because as per the concept we are told that the MACRO's are always replaced while COMPILE time and in the function we are having the value of (int * variable) being passed from some other function, and this operation would take place only during RUN-TIME, so how do we explain this scenario where the MACRO is replaced during OMPILE time and the value with which it is going to be replaced is got at the RUN-TIME? This is my main query, hope i did not confuse you! :wacky: please revert to me if more clarification is required for this question!

Thank you!:)

xpi0t0s 31Mar2009 14:13

Re: Related to #Define
 
> Hmm.. so we can declare define in a function like this!

No, you're not defining in a function. The preprocessor looks for # lines and processes them without regard to the code. So the preprocessor effectively sees the code as
Code:

blah blah blah
blah blah
#define one variable[0] - aha, a #define statement, so let's let "one" be defined as "variable[0]", whatever that means
#define two variable[1] - same again

blah blah
blah blah
printf("%d\n",one); - aha, there's a "one" here, we know what that is, so we'll change this line from 'printf("%d\n",one);' (whatever that means) to 'printf("%d\n",variable[0]);' (whatever that means)

blah
blah
Oh, end of file.  OK, I'll stop then.

So the preprocessor is REALLY DUMB. Also, after processing a # line it removes it from the source file and the compiler doesn't see it. So what the compiler sees is this:

Code:

int variable[2];

void test(int *variable) // rename variable to anything you like, it has no effect
{
}

int main()
{
variable[0]=5;
printf("%d\n",variable[0]);
return 0;
}

So that's what I meant when I said test() does nothing. You can see clearly here why it does nothing - because it contains no code.

As for whether or not it is good practice to do this, well I'd say it depends on the code. In this case it's confusing for the OP because he seems to think the "one" and "two" defines have something to do with the parameter passed into test(). If you have good reason to specify a #define within a function then do so, and usually what you would do is to add a #undef towards the end of that function so that the define doesn't hang around and cause problems for other people.

> what makes me confused is that if we are using a Macro then why is it required to make is Local to a function alone

You are quite right to be confused by that, because it's nonsense. You CANNOT make a #define local to a function, because the preprocessor does not take the language into account.

> I did not find this correct...

Sorry, I haven't got a clue what you're asking here. Macros are replaced at compile time - correct. The value of variable passed into test() is defined at runtime. "how do we explain..." - that's the bit I don't understand. What exactly needs explaining?

abhushan_s 31Mar2009 14:33

Re: Related to #Define
 
Hey thanks for getting it cleared for me!

lets go one by one,

1) The macro is defined and it is replaced by a particular value (in this case it is Variable[0] or variable[1]) during compile time, thats fine.
2) The main issue that iam facing is that the variable "Variable" is an Array, thats implicit, the next thing is that, initially when the program is compiled, as per the concept the pre-processor will replace all the Macros found in the program by their repective values as defined in the beginning (or in a function in this case, even i found that to be dumb :nice:, nonetheless may be that was required for that particular code), here as per the question the value of "Variable" (which is the pointer to the address of the value) is passed from another function or maybe Main(), and this is done during runtime (i hope iam not wrong here), so when we combine both the scenarios,

At one side we have a Macro being replaced but then with what will it be replaced is where iam getting the beating, ie., the array is initialized or formed during compile time but its content would be some garbage value as there is nothing in the array right now! I really hope you understood my confusion.

Please revert to me if any more clarification is required, as i need to get this thing clear in my mind! :crazy:

Thanks,
Abhushan.


All times are GMT +5.5. The time now is 10:50.