Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointer to an array, jumps an index every other time while looping

I'm trying to point a pointer to the middle of an array then loop indexes after and before that certain middle value using an algorithm, the algorithm is working fine, but when I try to loop the first index after the pointed one, my index jumps by 1 number, to be more precise, if we consider our pointer is pointing at index[0], I can use the value of index[0], then it goes to index[1], it thinks that it's empty (although it's not), then goes to index[2] and it can use it again, but then again, it thinks index[3] is empty, and this bug continues like a loop! I have tried everything but no matter what I do, it still skips index[1], I even have the -Wall and -g flags on and gcc doesn't give me any warnings either!

this is the code:

#include <stdio.h>

int main()
{
    char *alphabet[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
        "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

    char *p_alphabet = alphabet[12];

    int i;
    
    for (i = 0; i < 26; i++) {
        printf("%d: %c\n", i, p_alphabet[i]);
    }
}

and this is the output I get:

0: m
1:
2: n
3:
4: o
5:
6: p
7:
8: q
9:
10: r
11:
12: s
13:
14: t
15:
16: u
17:
18: v
19:
20: w
21:
22: x
23:
24: y
25:
like image 520
artin ghasivand Avatar asked Jan 31 '26 19:01

artin ghasivand


1 Answers

This declaration

char *p_alphabet = alphabet[12];

does not make a sense.

If you need a pointer to the middle element of the array then you have to write

char **p_alphabet = alphabet + 12;

Or it will be even more correctly to write

char **p_alphabet = alphabet + 13;
                              ^^^^

assuming that there are 26 initializers.

As this loop

for (i = 0; i < 26; i++) {
    printf("%d: %c\n", i, p_alphabet[i]);
}

iterates 26 times then this expression p_alphabet[i] that shall be written at least like p_alphabet[i][0] (taking into account the declaration I showed above) can access memory outside the array.

As for the output you got then it can be explained the following way. This declaration

char *p_alphabet = alphabet[12];

declares a pointer that points to the first character of the string literal "m" that is stored in memory as a character array with two elements { 'm', '\0' }.

Also it seems the compiler placed the string literals used as initializers of the array alphabet in the literal pool sequentially as they appeared in the initialing list.

So the memory can be represented like an extent of memory storing sequentially the following values

{ 'm', '\0', 'n', '\0, 'o', '\0', ..., 'x', '\0', 'y', '\0', 'z', '\0' }

and these values are outputted in your for loop. The value '\0' is not a graphical symbol. So you do not see it in the output.

If you want for example to output the array starting from its middle element and then outputting elements of the array alternatively that follow the middle element and that precede the middle element then the program can look the following way.

#include <stdio.h>

int main(void) 
{
    char * alphabet[] = 
    {
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
        "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
    };
    
    const size_t N = sizeof( alphabet ) / sizeof( *alphabet );
    
    char **p_alphabet = alphabet + N / 2;
    
    for ( size_t i = 0; i < N; i++ )
    {
        printf( "%2zu: %s\n", i, 
                             i % 2 == 0 ? *( p_alphabet + i / 2 )
                                        : *( p_alphabet - ( i + 1 ) / 2 ) );
    }
            
    return 0;
}

The program output is

 0: n
 1: m
 2: o
 3: l
 4: p
 5: k
 6: q
 7: j
 8: r
 9: i
10: s
11: h
12: t
13: g
14: u
15: f
16: v
17: e
18: w
19: d
20: x
21: c
22: y
23: b
24: z
25: a
like image 196
Vlad from Moscow Avatar answered Feb 03 '26 09:02

Vlad from Moscow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!