everton690
asked on
Quick question: Loop through int names
Hi,
just a quick problem that appears quite simple but is giving me trouble. All I want to do is loop through a group of int names and assign then a value. For example the int names are: int name1,name2,name3,name4 and the values to be assigned are 1,2,3,4. Is there anyway to loop through the names of the ints and assign them the correct value. Any ideas on how to correct below?. Thanks.
for(int value=1;value<5;value++){
name+1=value;
}
just a quick problem that appears quite simple but is giving me trouble. All I want to do is loop through a group of int names and assign then a value. For example the int names are: int name1,name2,name3,name4 and the values to be assigned are 1,2,3,4. Is there anyway to loop through the names of the ints and assign them the correct value. Any ideas on how to correct below?. Thanks.
for(int value=1;value<5;value++){
name+1=value;
}
Hi everton690,
from the code you posted it seems you are working in something like C/C++ language environment. correct??
leflon
from the code you posted it seems you are working in something like C/C++ language environment. correct??
leflon
U can Use Arrays.. that wud make it easier
Yep...
for(int value=1;value<5;value++){
int name[value]=value;
}
If you want name1, name2 ...then you have to make as Strings...by concatenating....
for(int value=1;value<5;value++){
int name[value]=value;
}
If you want name1, name2 ...then you have to make as Strings...by concatenating....
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well, but we're assuming that name1, name2, name3, name4 are allocated contiguous memory locations when they are allocated. What if they're not? The array approach is better.
ASKER
Thanks to all who contributed. I think I will use the poiner method although using an array would also be effective
Cheers,
everton690.
Cheers,
everton690.
everton690,
i would really recommend using the array way. using the pointer way can get you in trouble
if i take a look at my stack (where the vars name1 ... name4 are allocated) the order looks something like
address var
0x0012ff80 ?????? <------------------------- ---------- ---------- |
0x0012ff7c name1 <- nameptr -> nameptr++ ---
0x0012ff78 name2
0x0012ff74 name3
0x0012ff70 name4
.......
nameptr points to the address where var name1 is lying on the stack. incrementing the ptr will not bring you to name2 but to the address 0x0012ff80.
nobody knows what info is lying at that address. you can do real havoc to your prog if you now write something to this address. your prog may crash (which would be the best you can get) or just change some data belonging to a different part of your prog (very bad, ugly error, hard to detect in the long run). as mayankeagle said using this pointer method bases on assumption about the way the vars are allocated. i won' t rely on this.
do a little experimenting with pointers and the order you define the vars.
you will see using the array is the better and more secure way.
leflon
i would really recommend using the array way. using the pointer way can get you in trouble
if i take a look at my stack (where the vars name1 ... name4 are allocated) the order looks something like
address var
0x0012ff80 ?????? <-------------------------
0x0012ff7c name1 <- nameptr -> nameptr++ ---
0x0012ff78 name2
0x0012ff74 name3
0x0012ff70 name4
.......
nameptr points to the address where var name1 is lying on the stack. incrementing the ptr will not bring you to name2 but to the address 0x0012ff80.
nobody knows what info is lying at that address. you can do real havoc to your prog if you now write something to this address. your prog may crash (which would be the best you can get) or just change some data belonging to a different part of your prog (very bad, ugly error, hard to detect in the long run). as mayankeagle said using this pointer method bases on assumption about the way the vars are allocated. i won' t rely on this.
do a little experimenting with pointers and the order you define the vars.
you will see using the array is the better and more secure way.
leflon
ASKER
Thanks for the advice leflon, you saved a lot of my hair from almost certainly being pulled out while searching for errors that the pointer method would have introduced,
Cheers and thanks,
everton690
Cheers and thanks,
everton690
leflon:
You are right.
if i use something like:
char *abc = "Hello";
strcpy(abc+strlen(abc), "World!");
printf("\n%s",abc);
this piece of code works perfectly and prints HelloWorld!
but:
char abc1='H', abc2='e', abc3='l';
char abcptr = &abc;
for(int i=0; i<3; i++)
{
printf("%c",*(abcptr+i));
}
will printf---> H<garbage><garbage>
so can we reliably say that when independent variables are allocated, they are allocated one by one starting at higner memory address in stack like:
3000 abc1
2999 abc2
2998 abc3
whereas when memory is allocated as char array, it is allocated in bulk and the array name is made to point to the lower stack address like:
3000 abc[2]
2999 abc[1]
2998 abc[0] <---- using only abc will point here? i.e. abc without the subscript index?
in that case, doing strcpy on abc resulting in abc being larger than allocated array length will mean some other variables/unallocated space in higher memory of stack is overwritten, right?
i.e. in the above case, strcpy(abc,"HELLO"); will result in something like:
3002 'L' <unknown> ----these are illegal assignments that compiler wont catch
3001 'L' <unknown> ----i.e. array overflow
3000 'L' abc[2]
2999 'E' abc[1]
2998 'H' abc[0]
Please tell me if my analysis is correct, this will help everyone in using pointers and debugging!
;-)
Happy Programming!
Regards
-logicTRANCE
You are right.
if i use something like:
char *abc = "Hello";
strcpy(abc+strlen(abc), "World!");
printf("\n%s",abc);
this piece of code works perfectly and prints HelloWorld!
but:
char abc1='H', abc2='e', abc3='l';
char abcptr = &abc;
for(int i=0; i<3; i++)
{
printf("%c",*(abcptr+i));
}
will printf---> H<garbage><garbage>
so can we reliably say that when independent variables are allocated, they are allocated one by one starting at higner memory address in stack like:
3000 abc1
2999 abc2
2998 abc3
whereas when memory is allocated as char array, it is allocated in bulk and the array name is made to point to the lower stack address like:
3000 abc[2]
2999 abc[1]
2998 abc[0] <---- using only abc will point here? i.e. abc without the subscript index?
in that case, doing strcpy on abc resulting in abc being larger than allocated array length will mean some other variables/unallocated space in higher memory of stack is overwritten, right?
i.e. in the above case, strcpy(abc,"HELLO"); will result in something like:
3002 'L' <unknown> ----these are illegal assignments that compiler wont catch
3001 'L' <unknown> ----i.e. array overflow
3000 'L' abc[2]
2999 'E' abc[1]
2998 'H' abc[0]
Please tell me if my analysis is correct, this will help everyone in using pointers and debugging!
;-)
Happy Programming!
Regards
-logicTRANCE
and everton690, really sorry for misleading...i'll double check from now on...but i hope someone clarifies my above query..hope that helps all!
-logicTRANCE
-logicTRANCE
@logicTRANCE
> char *abc = "Hello";
> strcpy(abc+strlen(abc), "World!");
> printf("\n%s",abc);
is 'evil' out of the same reasons you stated in
> 3002 'L' <unknown> ----these are illegal assignments that compiler wont catch
> 3001 'L' <unknown> ----i.e. array overflow
> 3000 'L' abc[2]
> 2999 'E' abc[1]
> 2998 'H' abc[0]
there is no difference if you assign it by array-assignment or by copying it, as you stated you overwrite memory not belonging to the var. it is sheer luck that it didn't crash or misbehave.
the order a variable will be placed on the stack depends on the order they are defined in the prog.
int a,b,c;
will probably look like
1008 a
1004 b
1000 c
as the line is interpreted right to left (c -> b -> a).
while
int a;
int b;
int c;
should give something like
1008 c
1004 b
1000 a
(a -> b -> c)
> 3000 abc[2]
> 2999 abc[1]
> 2998 abc[0] <---- using only abc will point here? i.e. abc without the subscript index?
yepp!
relying on the order of vars in memory is not a way very good thing. try to avoid it - no, don't do it at all
cheers
leflon
> char *abc = "Hello";
> strcpy(abc+strlen(abc), "World!");
> printf("\n%s",abc);
is 'evil' out of the same reasons you stated in
> 3002 'L' <unknown> ----these are illegal assignments that compiler wont catch
> 3001 'L' <unknown> ----i.e. array overflow
> 3000 'L' abc[2]
> 2999 'E' abc[1]
> 2998 'H' abc[0]
there is no difference if you assign it by array-assignment or by copying it, as you stated you overwrite memory not belonging to the var. it is sheer luck that it didn't crash or misbehave.
the order a variable will be placed on the stack depends on the order they are defined in the prog.
int a,b,c;
will probably look like
1008 a
1004 b
1000 c
as the line is interpreted right to left (c -> b -> a).
while
int a;
int b;
int c;
should give something like
1008 c
1004 b
1000 a
(a -> b -> c)
> 3000 abc[2]
> 2999 abc[1]
> 2998 abc[0] <---- using only abc will point here? i.e. abc without the subscript index?
yepp!
relying on the order of vars in memory is not a way very good thing. try to avoid it - no, don't do it at all
cheers
leflon
hi leflon
this is what you wrote:
>int a,b,c;
>will probably look like
> 1008 a
> 1004 b
> 1000 c
>as the line is interpreted right to left (c -> b -> a).
>while
> int a;
> int b;
> int c;
>should give something like
> 1008 c
> 1004 b
> 1000 a
>(a -> b -> c)
i'd like to correct you:
int a,b,c; will look as you said alrite:
1008 a
1004 b
1000 c
your reason was: >as the line is interpreted right to left (c -> b -> a).
that is incorrect!
a is placed at higher memory and c at lower memory because stack always starts from higher memory. the compiler encounters a first and allocates memory for that first and decrements it's stack pointer(or data segment pointer; data segment is treated as stack). Then it encounters b and c respectively so they get successively lower addresses.
| |
| |
| |
0xffee | |
0xfff0 | c |
0xfff2 | b |
0xfff4 |_a_| <- base pointer of stack (data segment); the stack's offset pointer is always decremented, just before putting something on stack.
> int a;
> int b;
> int c;
>should give something like
> 1008 c
> 1004 b
> 1000 a
>(a -> b -> c)
this is incorrect! the stack still look the same, i.e.:
| |
| |
| |
0xffee | |
0xfff0 | c |
0xfff2 | b |
0xfff4 |_a_|
the reason is the same, a is encountered first, followed by b and c. that is the order in which stack memory is allocated.
I verified this with turboc debugger. can you tell me what you get on the system you are using?
ofcourse what you are saying can be a fact if the compiler chooses to interpret the line:
int a,b,c; in reverse order c->b->a!
but that entirely depends on how the compiler is designed!
i think there should be a standard for this issue. that will make using pointers much more safer... i'm not aware if there already is.... please do respond with your views
Regards!
-logicTRANCE
NOTE: int is allocated only 2 bytes since i'm using turboc, it'll be 4 bytes on 32bit compiler... but everything else will remain same if i'm correct.
this is what you wrote:
>int a,b,c;
>will probably look like
> 1008 a
> 1004 b
> 1000 c
>as the line is interpreted right to left (c -> b -> a).
>while
> int a;
> int b;
> int c;
>should give something like
> 1008 c
> 1004 b
> 1000 a
>(a -> b -> c)
i'd like to correct you:
int a,b,c; will look as you said alrite:
1008 a
1004 b
1000 c
your reason was: >as the line is interpreted right to left (c -> b -> a).
that is incorrect!
a is placed at higher memory and c at lower memory because stack always starts from higher memory. the compiler encounters a first and allocates memory for that first and decrements it's stack pointer(or data segment pointer; data segment is treated as stack). Then it encounters b and c respectively so they get successively lower addresses.
| |
| |
| |
0xffee | |
0xfff0 | c |
0xfff2 | b |
0xfff4 |_a_| <- base pointer of stack (data segment); the stack's offset pointer is always decremented, just before putting something on stack.
> int a;
> int b;
> int c;
>should give something like
> 1008 c
> 1004 b
> 1000 a
>(a -> b -> c)
this is incorrect! the stack still look the same, i.e.:
| |
| |
| |
0xffee | |
0xfff0 | c |
0xfff2 | b |
0xfff4 |_a_|
the reason is the same, a is encountered first, followed by b and c. that is the order in which stack memory is allocated.
I verified this with turboc debugger. can you tell me what you get on the system you are using?
ofcourse what you are saying can be a fact if the compiler chooses to interpret the line:
int a,b,c; in reverse order c->b->a!
but that entirely depends on how the compiler is designed!
i think there should be a standard for this issue. that will make using pointers much more safer... i'm not aware if there already is.... please do respond with your views
Regards!
-logicTRANCE
NOTE: int is allocated only 2 bytes since i'm using turboc, it'll be 4 bytes on 32bit compiler... but everything else will remain same if i'm correct.
ASKER
above should be something like:
for(int value=1;value<5;value++){
name+value=value;
}