Question: Got a silly question im struggling with.

Im trying to step through a C# Multidimensional array using nested for loops, but i cant get the result i want and im thinking its just a stupid problem with my code.

```
string search = txtString.Text;

        int iLoop;
        int jloop;
        int iResult = -1;

        for (iLoop = 0; iLoop < sounds.GetLength(0) ; iLoop++)
        {
            for (jloop = 0; jloop < sounds.GetLength(1) ; jloop++)
            {

                string result;
                result = sounds[iLoop,jloop];

                if (result == search)
                {
                    iResult = iloop;
                }
            }
        }

            if (iResult == -1)
            {
                MessageBox.Show("Result not found");
            }
            else
            {
                MessageBox.Show("Result found at position " + iResult);                    
            }            
    }

```

It searches the array, and returns a positive result if the answer is found, but the result position is always "Result found at position 1".

What have i done wrong?

Answer: 
This is [undefined behaviour](http://en.wikipedia.org/wiki/Undefined_behavior), which means anything can happen:

```
char *s, *t;
strcpy(s, "foo");

```

as `strcpy()` is writing to a random location in memory because `s` is an uninitialised pointer.

---

(*after edit that corrected undefined behaviour*)

> 
> **Question 1** - Why does value of t changes in this case? (I copied "bar" to s why did t change).
> 
> 
> 

This is a pointer assignment:

```
t = s;

```

and results in both `t` and `s` pointing to the same memory that was `malloc()` and assigned to `s` earlier. Any change to that memory is visible via both `t` and `s`.

> 
> **Question 2** - Why isn't t changing in the second case?
> 
> 
> 

This assigns the address of the string literal `"bar"` to `s`:

```
s = "bar";

```

so now `t` and `s` do not point to the same memory location. `t` points to the memory that was earlier `malloc()` and assigned to `s` (because of the `t = s;` pointer assignment).

`strcpy()` and `=` are *very* different:

* `strcpy()` copies characters to the memory address specified by its first argument
* assignment, `=`, changes the address which a pointer holds
a simplistic way to understand could be as follows :- 

```
      s = malloc(4 * sizeof(char));    // this will be interpreted as follows 
      s = 123 ------------>|   |   |   |   | //some garbage values at these location
                           123  124 125 126   // some random address chosen.

      strcpy(s, "foo") ; // it copies at the address provided by s i.e.
                             |'f'|'o'|'o'|'\0'|
                              123 124 125  126 

      t = s;      this statement means t it pointing to address = 123

      strcpy(s, "bar"); // this statement copies at given address it means it will   override previous value .   i.e
                            |'b'|'a'|'r'|'\0'|
                             123 124 125 126

```

now t still pointing to address 123 that is why t, s both prints bar . 

```
       s = "bar"  // this will  assign a new address to s which is base address of "bar" string . i.e .          
                           |'b'|'a'|'r'|'\0'|
                            321 322 323 324     

```

now s will contain address 321 while t has value 123 that is why s and t are giving different values .