LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-02-2007, 07:04 AM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Rep: Reputation: 30
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
 
Old 10-02-2007, 07:47 AM   #2
virtualCoder
Member
 
Registered: Sep 2007
Distribution: Ubuntu
Posts: 33

Rep: Reputation: 15
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.
 
Old 10-02-2007, 07:59 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 239Reputation: 239Reputation: 239
declarations should come first in a C block i.e. function.
you can't declare in a loop
 
Old 10-02-2007, 08:04 AM   #4
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks guys That solves my problem.
 
Old 10-02-2007, 08:57 AM   #5
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
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
 
Old 10-02-2007, 12:46 PM   #6
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
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
Code:
int i[3];
, 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:
Code:
int i;
Code:
i = 1;
, but I don't understand why this can't be done when using an array.
 
Old 10-02-2007, 01:10 PM   #7
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
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.
 
Old 10-03-2007, 02:54 AM   #8
devn
Member
 
Registered: Mar 2007
Location: Bangladesh
Distribution: Suse, Solaris
Posts: 40

Rep: Reputation: 15
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.
 
Old 10-03-2007, 03:48 AM   #9
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks guys It is clear to me now.

Regards,

Ben
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting: how can I reference a variable? frankie_DJ Programming 4 10-06-2006 06:13 AM
Variable scope Ephracis Programming 5 07-28-2006 02:22 PM
Variable available scope problem ArthurHuang Programming 1 05-22-2006 01:16 AM
java variable scope - use of "this" keyword zeppelin147 Programming 1 11-21-2005 11:04 PM
reference Variable in C++ atul_mehrotra Programming 6 10-06-2004 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration