1

When the arguments of dyn_mat are constants, the code runs through without any error and s1 and s2 do store the input values.

#include<stdio.h>

int main(int argc, char const *argv[])
{
    char *s1, *s2;
    int n1=7, n2=8;
    printf("Enter, %d \n", n1);

    scanf("%s", s1);
    scanf("%s", s2);

    int dyn_mat[155][347];

    return 0;
}

but with arguments as variables, say n1 and n2, scanf reading s1 gives segmentation fault.

tunetopj
  • 561
  • 2
  • 5
  • 15
  • 2
    C doesn't allow this. Your code has undefined behavior. – Mat Oct 27 '12 at 12:01
  • "When the [dimensions] of `dyn_mat` are constants, the code runs through without any error and `s1` and `s2` do store the input values. But with arguments as variables, say n1 and n2, scanf reading s1 gives segmentation fault." The code does run through with the same severe error in both cases, you just happen to not see its effects. – Sergey Kalinichenko Oct 27 '12 at 12:06

2 Answers2

7

The code simply has undefined behaviour, since s1 and s2 are not valid pointers. scanf expects a pointer to an array of chars that's large enough to hold the read data, and you are not providing such pointers.

The usual way would be something like this:

char s1[1000];
char s2[1000];

scanf("%s", s1);
scanf("%s", s2);

(Though you should use a safer version that specifies the available buffer size rather than hoping for the input to be sufficiently short; for example, scanf("%999s", s1);.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • but when i give constant arguments, why does it work every time and s1 and s2 always store the input values without any problem. – tunetopj Oct 27 '12 at 12:17
  • s1 and s2 point to some arbitrary area in memory. Your code overwrite whatever is present there. You don't see problems in **your program** because most likely the effect is somewhere else and corrupting something else in your system. – SomeWittyUsername Oct 27 '12 at 12:26
  • @icepack I'm not so sure it's corrupting something else in your system, and it's definitely not touching the OS. Processes have address space and an access violation would also throw a segmentation fault. See [What is a segmentation fault?](http://stackoverflow.com/a/2346849/1689220) – joe Oct 28 '12 at 14:26
  • @porkshoulder that actually depends on the running environment, what you're saying isn't correct for embedded or kernel code (or DOS! :) ). In this case, however, you're probably right since this looks like a standard user space code which runs in a protected environment so the corruption will occur inside the running process – SomeWittyUsername Oct 28 '12 at 14:31
0

why does c allow initialization of string without declaration?

There is no data type string in C.

In C one possible way to store a string of characters is using an array of characters, with the last element of this array carring a 0 to indicate the end of this string.

You program does not declare any array, but just pointers to characters, which have no memory assigned to which you copy data using scanf().

Your just lucky the program does not crash with the first call to scanf().

alk
  • 69,737
  • 10
  • 105
  • 255