1

There is 12-elements *prim array and there is initialized only 7 elements. Here is code:

int f(int input)
{
    char *prim[12] = {"2", "3", "5", "7", "11", "13", "17"};
    int i;

    for(i=0; i<12; i++)
    {       
        if(i % input == 0)
        {
            strcat(result, prim[i]);
            strcat(result, " ");
        }
    }

    if(strlen(result) == 0)
    {
        printf("return -1");
    }
    else 
    {
        printf("%s\n", result);
        printf("return 0");
    }
}

When i = 8 debugger give me an Access violation reading location.

My friend gave me this sample of exam in programming basics course on his college, and I need to help him passing it.

So, my questions is: is there something i don't know about programming in C, or the professor made a mistake in declaring *prim?

davor
  • 939
  • 2
  • 14
  • 31
  • 1
    What is your real question? I am almost certain that there are things that you don't know about C. – Jens Gustedt Sep 16 '12 at 11:29
  • Was there a question in the exam paper to go alongside the code? Maybe it was one of those questions about how to fix broken code? – theon Sep 16 '12 at 11:32
  • possible duplicate of [initialize array to 0 in C](http://stackoverflow.com/questions/2589749/initialize-array-to-0-in-c) – Pascal Cuoq Sep 16 '12 at 11:33
  • Another candidate duplicate for this question is http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c – Pascal Cuoq Sep 16 '12 at 11:34
  • I didn't copied wrong code, it's on the paper in front of me. Question in exam is: If input = 2, what is the result of function? I know that other values are 0, but this is really basics exam, there are writing answers with pencil, and in every other assignment of this type the result is clear (int value or strings array) – davor Sep 16 '12 at 11:39
  • @JensGustedt - i am certain that i don't know much about programming in c, too. i was referring to my knowledge of arrays in programming, with regard to arrays in c. – davor Sep 16 '12 at 11:49
  • _I didn't copied wrong code_ you did. And then you edited it after my answer. So it's questionable if what we see now is the whole exact original code or it's something different that **you** think is equivalent. – Analog File Sep 16 '12 at 12:12

4 Answers4

6
char *prim[12] = {"2", "3", "5", "7", "11", "13", "17"};

The remaining five elements are initialized with null pointers as if you declared:

char *prim[12] = {"2", "3", "5", "7", "11", "13", "17", 0, 0, 0, 0, 0};

Calling strcat(result, prim[i]); with a null pointer argument is undefined behavior.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • @KerrekSB, right and wrong. A null pointer, according to the C standard, is any constant integer expression of value `0`, maybe cast to a pointer type. The macro `NULL` resolves to one of these possibilities. Common values for `NULL` are `(void*)0` and `0`. – Jens Gustedt Sep 16 '12 at 12:27
  • 2
    @KerrekSB: See [6.3.2.3/3](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). In a pointer context, `0` and `NULL` are the same thing. – John Bode Sep 16 '12 at 13:26
  • 1
    OK, fair enough. I'm always thinking how a literal would do in a deduced context (like a template, `auto` or `decltype`), and that's why I'd always prefer the "type-safe" constant, if you will... but you're both right of course. – Kerrek SB Sep 16 '12 at 13:32
5

If an initializer is omitted in an array intialization, the corresponding place is initialized with 0. So here all the missing char pointers are initialized with 0. Trying to derefference these null pointers crashes your program.

If this was unexpected behavior for your prof, if you simply copied wrong from the blackboard, or if there is some other error, I can't know.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
1

Location 8 and onwards are initialized to 0 (NULL pointer), see e.g. this section of C lecture notes, strcat with NULL pointer is undefined and will result in segmentation fault on many common systems, or in your case in access violation reading location as strcat attempts to read at zero address.

artm
  • 3,559
  • 1
  • 26
  • 36
0

Question in exam is: If input = 2, what is the result of function?

There's something very fishy here (which may well be the whole point of the exam).

In any case here are my observations:

  • EDIT: the OP was edited, so this is no more the case. The function you posted does not use the input parameter at all. So what it does and it's result (for any definition of result) is independent on the parameter.

  • The function is declared to return an int, but there's no return statement. Any decent compiler will give you at least a warning about that.

  • EDIT: the OP was edited, so this is no more the case, but it's still true if the argument is 2 as for the comments. The function is guaranteed to segfault if strcat refers to the standard library function, because the function eventually calls it with the second argument being NULL.

  • Even if the function, for some reason, did not segfault, it is unclear what result means. In terms of side effects the function may print some stuff to standard output. In terms or returned value the function will return an integer, but lacking a return statement there's no way to know what that integer is. It effectively is a random value that depends on the implementation.

Analog File
  • 5,280
  • 20
  • 23
  • i agree with you, besides 1st point: input parameter is used inside for loop ( if(i % input)...). i replaced it with 2 while i was testing function. – davor Sep 16 '12 at 12:02