assign array with a local var???


 
Thread Tools Search this Thread
Top Forums Programming assign array with a local var???
# 1  
Old 04-05-2011
assign array with a local var???

Hi all,

I am just wondering is this (below code) valid in programming
Code:
   int xx = 10110;
   int i;
   long yy[] = {1,2,3,4,5,6,7,8,xx};

I compiled this using gcc will all working option successfully without getting any warning or error. I didn't see this kind of code in my past experience. Please comment the cons if any

Thanks and Regards...

Last edited by Franklin52; 04-05-2011 at 07:33 AM.. Reason: Fixed code tags
# 2  
Old 04-05-2011
I believe it conforms to C99, but not C89 - you'll get an error if you compile in 89 mode with the -pedantic flag.

I only see cons if you're going to port to a compiler that doesn't support this. Otherwise, you might as well take advantage of what the compiler can do for you to make your life easier.
This User Gave Thanks to JohnGraham For This Post:
# 3  
Old 04-05-2011
Thanks John, yes with -pedantic option it reporting following warnings
Code:
cc1: warnings being treated as errors
r.c: In function ‘main':
r.c:7: warning: initializer element is not computable at load time

surely this will creating issues at the time of porting

Last edited by pludi; 04-05-2011 at 08:21 AM..
# 4  
Old 04-05-2011
Quote:
Originally Posted by zing_foru
surely this will creating issues at the time of porting
Not necessarily - it's valid C99 (I'm pretty sure...), so any compiler that supports that should have no problem with it.
# 5  
Old 04-05-2011
I think if it's a const int that should be permissible, since that would make xx guaranteed computable not just load-time but compile-time...
# 6  
Old 04-05-2011
Quote:
Originally Posted by Corona688
I think if it's a const int that should be permissible, since that would make xx guaranteed computable not just load-time but compile-time...
Unfortunately not - in c89 initialisers must be constant expressions. A constant expression is one that only uses literal values and arithmetic operators, such as "2" or "3 * 2", not "3 * x", even if x is declared a const variable.

"const" does not mean "constant" - it merely means that the const variable itself cannot modify the memory at that address. Others can.
# 7  
Old 04-05-2011
Quote:
Originally Posted by JohnGraham
"const" does not mean "constant" - it merely means that the const variable itself cannot modify the memory at that address. Others can.
True for stack variables but not for static or global variables or literal strings, these frequently have hardware protection.

In any case that's besides the point. The compiler treats constant variables as constants, which was what I'd hoped would allow this assignment behavior. Oh well.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign two dimensional array in bash at once

Hi, I have a 10*10 two dimensional array. How do I assign value to all it's 100 elements at once? I don't want to open two for loops and assign one by one. Thanks, Amit (2 Replies)
Discussion started by: amit14august
2 Replies

2. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

3. Shell Programming and Scripting

Assign value to array separated by #

Hi all , I have a string like para1#para2#para3 i want to assign para1 as first element para2 as second and so on i tried IFS=# set -A array para1#para2#para3 echo ${array} para1 para2 para3 i want echo ${array} para1 (2 Replies)
Discussion started by: max_hammer
2 Replies

4. UNIX for Advanced & Expert Users

assign array with a local var???

topic moved to programming (0 Replies)
Discussion started by: zing_foru
0 Replies

5. Shell Programming and Scripting

assign value to array variable

Hi, I have a piece of code as follows: i=0 while read LINE do var = "$LINE" i=$((i+1)) echo "${var}" done < file I want to assign value to the array var. However, when i execute the script i get a error. Please can you help me know what i am missing. I ultimately want to... (2 Replies)
Discussion started by: sunrexstar
2 Replies

6. Shell Programming and Scripting

assign var with set=a[5] not working

Hi Experts, I'm having issue in assigning var with special character , please see below for complete details. $ echo $SHELL /bin/csh $ cat bp abd/asd/a $ awk -F "/" '{print $NF}' bp | awk '{print $1}' a $ set a=`awk -F "/" '{print $NF}' bp | awk '{print $1}'` $ echo $a ... (15 Replies)
Discussion started by: novice_man
15 Replies

7. Shell Programming and Scripting

How to assign a variable to an array

I want to ask the user to enter an X amount of file names. I want to put those names into an array and then loop back through them to verify they are in the directory. 1st- How would I assign the value to an array and what is the correct syntax. 2nd- how would i reference that array after I... (3 Replies)
Discussion started by: tvb2727
3 Replies

8. Shell Programming and Scripting

Assign words in a string to array

I have a string as "yes why not" I want to create one array variable with contents as one word per place in array.. for above string,the array variable should contain... x="yes,why,not" x = yes x = why x = not Please help me,I am stuck up in the problem since 2 days... (3 Replies)
Discussion started by: uday26
3 Replies

9. Shell Programming and Scripting

SSH script. (Assign $var to free cmd) ???

Running: GNU/Linux, 2.6.22.9-61.fc6 (hope that's the right info needed) I am trying give the amount of RAM and the % used. I am using free command. I am having some problems with this command code: 1)T = `free | grep Mem | awk '{print $2}'` F = `free | grep Mem | awk '{print $4}'`... (3 Replies)
Discussion started by: AngelFlesh
3 Replies

10. Shell Programming and Scripting

assign awk array with printf

I am trying to assign a awk array for further processing later in the script. I can't seem to figure it out. If someone could look at this and help me, I would very much appreciate it. Thanks in Advance. for ( x = 1 ; x <= Var ; x++ ) { if ( x in varr ) { ... (2 Replies)
Discussion started by: timj123
2 Replies
Login or Register to Ask a Question