Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-02-2007, 07:04 AM
|
#1
|
Member
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164
Rep:
|
How to reference a variable outside it's block scope in C
Hi
I want to make use of a variable outside of the for loop in C from where it is initialized. I wonder what a common solution is for that situation. I am trying to achieve this with the static specifier but that does not work the way I implement it:
Code:
#include <stdio.h>
main()
{
int i;
for(i=0;i<5;i++){
static int k = 1;
}
printf("%s", k);
return 0;
}
I get an "'k' undeclared" error.
So what is a common solution in this situation, and what would be the proper way of using the static specifier in this case?
Thanks
|
|
|
10-02-2007, 07:47 AM
|
#2
|
Member
Registered: Sep 2007
Distribution: Ubuntu
Posts: 33
Rep:
|
Hi,
Well first of all this line seems to be incorrect:
printf("%s", k);
since %s is used for strings, and k is an integer.
Second, well you can refer to a variable outside the scope of a for loop by declaring it
outside the scope of a for loop.
So for example:
Code:
// Declare i and k.
int i, k;
for (i=0;i<5;i++) {
// Scope of for loop in here.
k = 1;
}
// Use i and k outside scope of for loop since they existed outside in the first place.
printf("i = %d, k = %d\n", i, k); // Will print "i = 5, k = 1".
Hope that clears your problem.
|
|
|
10-02-2007, 07:59 AM
|
#3
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
declarations should come first in a C block i.e. function.
you can't declare in a loop
|
|
|
10-02-2007, 08:04 AM
|
#4
|
Member
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164
Original Poster
Rep:
|
Thanks guys That solves my problem.
|
|
|
10-02-2007, 08:57 AM
|
#5
|
Senior Member
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994
Rep:
|
Actually, in ANSI C you can declare a variable inside a loop or block:
Code:
#include <stdio.h>
int main() {
int i;
for (i=0; i < 10; ++i) {
int j=i;
printf("%d\n", j);
}
return 0;
}
This compiles and runs without warnings:
Code:
$gcc test.c -o ./test -Wall -ansi && ./test
0
1
2
3
4
5
6
7
8
9
The important thing to remember is only to declare variables at the start a block, and they are not visible outside of that block.
Of course, there is an argument that if you can put a variable inside a block inside a method then that block should probably be a method in itself.
—Robert J Lee
|
|
|
10-02-2007, 12:46 PM
|
#6
|
Member
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164
Original Poster
Rep:
|
Thanks rjlee.
I have run into another problem. When I declare an array, I am unable to initialize it later on. Normally I just did something like
Code:
int i[3] = { 1, 2, 3 };
, but when I declare i first now, like , I am unable to intialize it afterwards. So can someone explain to me how this can be done? I know it can be done like this when you intitialize a variable with just one integer: , but I don't understand why this can't be done when using an array.
|
|
|
10-02-2007, 01:10 PM
|
#7
|
Member
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205
Rep:
|
Hi,
Code:
int main()
{
int i[3];
i[0] = 1;
i[1] = 2;
i[2] = 3;
}
-- OR --
int main()
{
int i[3];
int j;
for ( j = 0; j < 3; j++ )
i[j] = j;
}
You get the idea.
|
|
|
10-03-2007, 02:54 AM
|
#8
|
Member
Registered: Mar 2007
Location: Bangladesh
Distribution: Suse, Solaris
Posts: 40
Rep:
|
array assignment
Hi,
Try is, if it helps your case anyhow...
Code:
#include <stdio.h>
int main()
{
int i[3];
i[1]=1200; // assign 1200 to 2nd index of array
printf("%d",i[1]);
return 0;
}
I guess this is what you already know, but anyway, since all the entities within array are treated as single intergers, just attached together (actually addresses of all the integers with array are allocated at runtime to link them in an integer), you can manipulate any index of the array just by pointing your index at it.
|
|
|
10-03-2007, 03:48 AM
|
#9
|
Member
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164
Original Poster
Rep:
|
Thanks guys It is clear to me now.
Regards,
Ben
|
|
|
All times are GMT -5. The time now is 10:04 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|