0

I want ask about define array of pointers in C. In following code, I define two pointers to char and then I assign each pointer to return from strtok function.

#include<stdio.h>
#include<string.h>
main()
{
char *s[2], string[]="hehe hihi keke kaka huhu hixhix",delimit[]=" ";
int i=0;
        s[i]=strtok(string,delimit);
        while( s[i]!=NULL )
        {
                printf("i=%d -> %s \n",i,s[i]);
                ++i;
                s[i]=strtok(NULL,delimit);
        }
}

This is output:

i=0 -> hehe 
i=1 -> hihi 
i=2 -> keke 
i=3 -> kaka 
i=4 -> huhu 
i=5 -> hixhix 

So, I just define two pointers but the code run with no error when i is greater then 1.

Why it work well even when I just define less number of pointers than necessary pointers (eg: 6 or greater) ?

thanks for reading!

user173717
  • 73
  • 1
  • 10

2 Answers2

3

Using an invalid index of an array will lead to Undefined Behavior which means that anything can happen. It needn't necessarily "work" as expected or not, or make the program crash or format your hard disk. Anything can happen.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
  • 2
    Add a part about the *most likely* outcomes in this case and why they have their probabilities to earn my ⁺¹. – Alfe Apr 13 '15 at 14:48
  • I don't know which are the outcomes that are more probable to happen. Is it a crash or something else? – Spikatrix Apr 13 '15 at 14:59
  • Well, I'd say that "results as expected" is a very probable outcome in this small, independent, short-running example (because memory management will likely not notice the bug, so it will not let the process segfault, and nothing else of the program's memory is going to be noticeably corrupted by the bug). Reasoning this might make this answer a great answer! – Alfe Apr 14 '15 at 09:10
  • (Especially you should reason why "format your hard disk" is extremely unlikely ;-) – Alfe Apr 14 '15 at 09:20
3

Sometimes, it is the case that when you are out of the array area you are in fact changing some unhappy neighbor variable, or some return address. (In your example you are probably destroying "string" after used.)

In this situation "working well" is much worst then a segmentation fault.

The error, the variable changed, will come to you later, with some unpredictable random behavior always in the worst moment, ( similar situations, were detected in some very big disasters )

ryyker
  • 22,849
  • 3
  • 43
  • 87
JJoao
  • 4,891
  • 1
  • 18
  • 20
  • You could always provide a link to support your siting of similar situations. – ryyker Apr 13 '15 at 15:04
  • @ryyker, could you help me with that? I studied something about an overflow in Ariane5, about a control system of a nuclear central (this time a last minute code reformatting), a problem with the sw responsible for controlling the acceleration of a very popular car. But I don't have the correct details, and I am afraid of being melodramatic... – JJoao Apr 13 '15 at 15:15
  • Edited a link into your answer... :) A little melodrama, every now and then, is good for the soul. – ryyker Apr 13 '15 at 16:05