how to call a variable without reference in c

A

anto frank

hi friends,

is it possible to call a variable from the main function to a sub
fuction with out sending as a argument, pointer and can't declare as
global variable?

void main()
{
int a=10;
fun();
}
fun( )
{
printf("%d", a);
}

condition
1. don't use pointer concept
2.don't declare as global variable
3.don't send as arguments

IS IT POSSIBLE?

please help me
 
U

user923005

hi friends,

is it possible to call a variable from the main function to a sub
fuction with out sending as a argument, pointer and can't declare as
global variable?

void main()

The main() function returns int.
{
int a=10;
fun();
}
fun( )
{
printf("%d", a);

}

condition
1. don't use pointer concept

It's going to be pretty hard to print without any pointers.
2.don't declare as global variable
3.don't send as arguments

IS IT POSSIBLE?

All things are possible except those that aren't.

For instance, write a file in function 1.
Read the file from function 2.
 
M

Martin Ambuhl

anto said:
hi friends,

is it possible to call a variable from the main function to a sub
fuction with out sending as a argument, pointer and can't declare as
global variable?

You should start by learning the very elementary, most trivial parts of
the language before playing games with trying to circumvent the
mechanism for passing (not "calling") arguments to functions. For example:
void main()
^^^^
This marks you immediately as ignorant. Now, ignorance is no sin: we
all are ignorant of many things, and this deficiency can be easily
overcome. In this case, turn to the very first pages of any textbook on
C written by even a semi-competent C programmer. main returns an int.
{
int a=10;
fun();

main returns an int; you ought to have it actually do so.
}
fun( )
{
printf("%d", a);
^^^^^^
printf is variadic and must have a declaration visible. You have none.
The simplest way to provide it is with
#include <stdio.h>
And 'a' in main() has scope only of the block in which it is declared.
The 'a' in fun() has no declaration and your compiler ought to have told
you so.
Implicit int has been disallowed for eight years now. If fun returns an
int, you should say so.
}

condition
1. don't use pointer concept
Then you can't even call printf, since its first argument is a pointer.
2.don't declare as global variable
3.don't send as arguments

IS IT POSSIBLE?

please help me

Why should you want to do such a worthless thing?
 
K

karthikbalaguru

hi friends,

is it possible to call a variable from the main function to a sub
fuction with out sending as a argument, pointer and can't declare as
global variable?

void main()
{
int a=10;
fun();
}
fun( )
{
printf("%d", a);

}

condition
1. don't use pointer concept
2.don't declare as global variable
3.don't send as arguments

IS IT POSSIBLE?

please help me

It is Funny, But Some ways are there :):):)
1)Write a into an external memory in main before calling fun and read
from the external memory in fun before printing it.
2)Do a bit of assembly and push it in memory stack explicitly and pop
it out explicitly using assembly in the fun.
3)Copy to free register of the CPU and read it out from there.

Karthik Balaguru
 
R

Richard

Martin Ambuhl said:
You should start by learning the very elementary, most trivial parts
of the language before playing games with trying to circumvent the
mechanism for passing (not "calling") arguments to functions. For
example:
^^^^
This marks you immediately as ignorant. Now, ignorance is no sin: we

No. This marks him as uninformed. We can play word games, but "ignorant"
is rude and offensive. Blame the course work. Of course, it could be a
troll too. It has all the marks of being so bad while appealing to
almost anyone to correct since it's just more of the sameo, sameo basic
errors. Lets tick off the usual suspects:

1) Appears to be homework : check
2) Incorrect main() declaration : check
3) lack of standard include : check
4) Use of the word "friends" : check
5) spelling of function as fuction : check
6) mention of global variables : check

Troll % chance : 89.38784%
all are ignorant of many things, and this deficiency can be easily
overcome. In this case, turn to the very first pages of any textbook
on C written by even a semi-competent C programmer. main returns an
int.


main returns an int; you ought to have it actually do so.

Not in his declaration it didn't. Main *should* return an int. Different
thing.

^^^^^^
printf is variadic and must have a declaration visible. You have
none. The simplest way to provide it is with
#include <stdio.h>
And 'a' in main() has scope only of the block in which it is
declared. The 'a' in fun() has no declaration and your compiler ought
to have told you so.
Implicit int has been disallowed for eight years now. If fun returns
an int, you should say so.
Then you can't even call printf, since its first argument is a pointer.


Why should you want to do such a worthless thing?

Homework I would guess. Or a troll.
 
I

Ivan Gotovchits

anto said:
hi friends,

is it possible to call a variable from the main function to a sub
fuction with out sending as a argument, pointer and can't declare as
global variable?

void main()
{
int a=10;
fun();
}
fun( )
{
printf("%d", a);
}

condition
1. don't use pointer concept
2.don't declare as global variable
3.don't send as arguments

IS IT POSSIBLE?

please help me
I think that the idea is to access it through stack. Variable a lives in
stack in a range of 4 bytes, because there is a Return Address. (It may be
wrong, I'm not sure).
So you can try to address it in this way:

int
main(void)
{
int a = 10;
func();
}

int
func(void)
{
int *p;
p -= 2; /* a return address and p itself (8 bytes total) */
printf("%d\n", *p);
}

I am not sure that it will work, but you can try it. And if it fails, then
google for `buffer overflow'.
 
F

Flash Gordon

Ivan Gotovchits wrote, On 25/09/07 16:29:
I think that the idea is to access it through stack. Variable a lives in
stack in a range of 4 bytes, because there is a Return Address. (It may be
wrong, I'm not sure).

It might on your implementation, or it might not. The compiler could
even not bother allocating any memory for a.


One requirement was to no use pointers.
p -= 2; /* a return address and p itself (8 bytes total) */

p is unititialised and so could contain anything.
printf("%d\n", *p);
}

I am not sure that it will work, but you can try it. And if it fails, then
google for `buffer overflow'.

Your suggested code is almost certain to fail for many reasons.
 
K

Keith Thompson

Ivan Gotovchits said:
I think that the idea is to access it through stack. Variable a lives in
stack in a range of 4 bytes, because there is a Return Address. (It may be
wrong, I'm not sure).
So you can try to address it in this way:

int
main(void)
{
int a = 10;
func();
}

int
func(void)
{
int *p;
p -= 2; /* a return address and p itself (8 bytes total) */
printf("%d\n", *p);
}

I am not sure that it will work, but you can try it. And if it fails, then
google for `buffer overflow'.

I'm afraid your proposed solution is nonsense. The variable 'p' is
uninitialized. Its initial value is garbage; even examining it
invokes undefined behavior. You assume that ints and pointers are the
same size, something that is not guaranteed by the language (and it's
untrue on several systems I use every day). Even if that were valid,
you make several unsupportable assumptions about stack layout (there
might not even be a stack in the usual sense).

Did you try it yourself? Why not?
 
M

Mark McIntyre

hi friends,

is it possible to call a variable from the main function to a sub ....
condition
1. don't use pointer concept
2.don't declare as global variable
3.don't send as arguments

"Is it possible to eat sausages ?
Condition:
1. don't kill pig
2. don't construct sausage
3. don't send to mouth"

:)

To answer your question - yes, sure. Just write the data to a file,
then read it in again. Ah, no - this needs a file pointer. Oh well.

If this is homework, your teacher needs upgrading....
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
W

Walter Roberson

is it possible to call a variable from the main function to a sub
fuction with out sending as a argument, pointer and can't declare as
global variable?

We get this question posed to us every few months; you could probably
find old discussions of it via google groups.

If I recall correctly (and I did not pay close attention), the
answer being looked for by the people who framed the question, was
to declare a local array in the subfunction and then to index it
beyond the end of the array in order to gain access to data on
a stack. Which end of the array you have to run off depends on
the details of the architecture, but the whoever wrote the
question assumed that everyone used the same architecture and
assumed that stacks were present and assumed that running off
the end of the array would work, and probably various other
poor assumptions too.
 
U

user923005

We get this question posed to us every few months; you could probably
find old discussions of it via google groups.

If I recall correctly (and I did not pay close attention), the
answer being looked for by the people who framed the question, was
to declare a local array in the subfunction and then to index it
beyond the end of the array in order to gain access to data on
a stack. Which end of the array you have to run off depends on
the details of the architecture, but the whoever wrote the
question assumed that everyone used the same architecture and
assumed that stacks were present and assumed that running off
the end of the array would work, and probably various other
poor assumptions too.

Of course that is nothing more than undefined behavior.

I guess that according to the original formulation you could use a
static variable (after all, it only has file scope and not global
scope).
 
R

Richard Bos

We get this question posed to us every few months; you could probably
find old discussions of it via google groups.

If I recall correctly (and I did not pay close attention), the
answer being looked for by the people who framed the question, was
to declare a local array in the subfunction and then to index it
beyond the end of the array in order to gain access to data on
a stack. Which end of the array you have to run off depends on
the details of the architecture, but the whoever wrote the
question assumed that everyone used the same architecture and
assumed that stacks were present and assumed that running off
the end of the array would work, and probably various other
poor assumptions too.

And didn't understand that running off the end of an array is the same
thing as using a wild pointer, so this solution actually doesn't conform
to the demands. But then, you have to understand C to realise that, and
the OP's teacher plainly doesn't.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top