int i;
int i2;
printf("enter char array amount\n");
scanf("%d",&i);
char char1[i];
char1[0] = 'A';
for(i2=0;i2<i;i2++){
char1[i] = char1[i-1]++;
}
for(i2=0;i2<i;i2++){
printf("%c\n",char1[i2]);
}
#assign char array by loop, then out output in order by loop
54 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
a bit confused so far
considering char1[i] = char1[i-1]++; is UB - so am i
what are you trying to do exactly?
so, to input number,
then output from A to x depending on input
so if 7 is entered
output:
A
B
C
D
E
F
G
do you have to use an array for that? why don't you just print it one char at a time?
i could, but the array version is more compact, its part of the excercise
is more compact
its most certainly not, but given its an exercise - it doesn't really matter
regardless (assuming you're fine with VLAs) - consider the value of i in this loop
for(i2=0;i2<i;i2++){
char1[i] = char1[i-1]++; // whats the value of i here?
}```
no. its whatever your user supplied you with
what does that have to do with the value of i?
int i;
printf("enter char array amount\n");
scanf("%d", &i);
char char1[i]; // VLA
...
for(int j = 0; j < i; j++){
char1[i] = char1[i-1]++;
}```
does this makes it clearer?
j and i are 2 different variables. i was never changed (i.e. its equal to the last index of the array + 1) yet the loop uses i to access the array for some reason
no. its not
at this point i suggest you run through that snippet with either a pen & paper / through a debugger and see what is the value of i throught the iteration
char1[1] = char1[1]+ char1[-1];
which is sopposed to set to 'B', but it makes it a 'Y' instead
thats still UB. you're not allowed to access char[-1]
please
and after you've done that - i suggest you first figure out how to print the chars rather than storing them in the array first
if you won't actually run through your code with either pen & paper or a debugger - you'll likely find it very hard to guess. please do so
char1[0]++;
changed from A to B
so how would you make array[0] (which is A) an array[1] to a B
i would use the correct index in a loop
ye you need the correct loop, also you need the assignment within the loop
sure. compare this loop to yours
for(size_t i = 0; i < size; i ++) {
arr[i] = 'A' + ...;
}```
sure. i suggest you won't do it that way. refer to my loop above
its a value you need to figure out
are you just guessing?
ive figured it out
how do i show the code on here?
int i2;
printf("enter char array amount\n");
scanf("%d",&i2);
char char1[i2];
char1[0] = 'A';
for(i=1;i<i2;i++){
char1[i] = (char1[i-1])+1;
}
for(i=0;i<i2;i++){
printf("%c\n",char1[i]);
}```
@ember vine Has your question been resolved? If so, type !solved :)
!solved