473,473 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to call a variable without reference in c

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

Sep 25 '07 #1
7 2304
On Sep 24, 11:19 pm, anto frank <j.antofrank...@gmail.comwrote:
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.

Sep 25 '07 #2
anto frank <j.************@gmail.comwrites:
[...]
condition
1. don't use pointer concept
2.don't declare as global variable
3.don't send as arguments
4. Do your own homework.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 25 '07 #3
Keith Thompson <ks***@mib.orgwrote:
anto frank <j.************@gmail.comwrites:
[...]
condition
1. don't use pointer concept
2.don't declare as global variable
3.don't send as arguments

4. Do your own homework.
5. And while you're at it, kick your teacher. It's a stupid question.

Richard
Sep 25 '07 #4
On Sep 25, 11:19 am, anto frank <j.antofrank...@gmail.comwrote:
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


Sep 25 '07 #5
anto frank wrote:
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'.
--
s/.../.gotovchits/g for email.
Sep 25 '07 #6
Ivan Gotovchits <iv*************@auriga.ruwrites:
anto frank wrote:
> 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

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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 25 '07 #7
On Tue, 25 Sep 2007 06:19:27 -0000, in comp.lang.c , anto frank
<j.************@gmail.comwrote:
>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
Sep 25 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mario Thiel | last post by:
Hello, i am new to JavaScript and right now i am learning Call by Value. i tryed to write a small script but somehow it doesnt work right. i cant put <br> into it, and i can only put the...
4
by: mangi03 | last post by:
Hi, I came acrosss g++ compile errors whenever I make a function call by reference and found out from the test program that compiler is treating the function argument differently when another...
2
by: Kench | last post by:
I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
1
by: Yarco | last post by:
$stdObject->sayHi = create_function('', 'print "Hi";'); How to call sayHi without do something like: $sayHi = & $stdObject->sayHi; $sayHi(); Any good idears?or no idear.
4
by: Ty | last post by:
Hello all, I am creating a web site with Visual Stuido 2008. I am trying to use a java script file to create a busybox for login from this page http://blogs.crsw.com/mark/articles/642.aspx. I...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.