hey guys i know its kinda noobish not to understand them since i been coding for 1 month but there rlly some stuff which confuses me in multidimensional array and nested loops like here in this program
I wanted to know if i sum up like in the array for example num[5][10] i wanted to know which get summed first if i did total+=num[x]; but here in this program i made i got weird error which i think shouldn't produce it so please someone enlighten me because till now i understand all stuff pointers memory management and hard stuff in C but i don't understand these multi and nested loops

#include <stdio.h>
int main(void)
{
    int num[2][3]= {
    {1,1,1},{1,1,1}
    };
    int i;
    int x;
    int total=0;
    for(i=0;i<2;i++) 
       for(i=0;i<3;i++) {
            total+=num[i][x];
            }
       printf("%d",total);
    return getchar();
}

shouldnt it produce 6 ? please someone enlighten me in understanding this ....

the error is probably due to the fact that 'x' isn't initialized, so it's value is something like negative eleventy thousand, causing your program to crap the bed when it uses it as one of the array indices.

You have an (i) problem.
You need to use two different variables within a nested loop, such as i and j.

for(i=0;i<2;i++) 
//       for(i=0;i<3;i++) {
         for(x = 0; x < 3; x++ ) {
            total+=num[i][x];
            }
Comments
redundant and obvious, how about reading the previous posts?

Whats with return getchar() ? You got several other ways to pause the output.Use one of them rather than this because even though you return some random character as the return of main and exit the OS thinks that the process didn't terminate properly because of a non zero return value.Which of course doesn't cause any problem here but would definitely cause great problems in future when you write some important code.
So better correct it now.

Find the corrected code.nothing to wierd about this.try to find out values of each elemnts manuall before
writing the code.

#include <stdio.h>
int main(void)
{
    int num[2][3]= {
    {1,2,3},{4,5,6}
    };
    int i;
    int x;
    int total=0;
    for(i=0;i<2;i++) 
      [RED] for(i=0;i<3;i++)[/RED] //here i  SHOULD BE x
           {
            total+=num[i][x];
            }
       printf("%d",total);
    return getchar();
}

int num[2][3]= {
    {1,2,3},{4,5,6}
    };

Value assinged like this:
===========================
num[0][0]=1;
num[0][1]=2;
num[0][2]=3;
num[1][0]=4;
num[1][1]=5;
num[1][2]=6;

assign the range of loop variables according to this.1st do a dry run in case of multiD arrays.

Thanks,
DP

Comments
What has been said two times before?

yah i know now but what if i wanna count like row by row like
{1,3,4} {3,5,6} it will count 1 + 3 and 3+5 and 6 + 4 ?
should i switch variables x first then i 2nd ?

Whats with return getchar() ? You got several other ways to pause the output.Use one of them rather than this because even though you return some random character as the return of main and exit the OS thinks that the process didn't terminate properly because of a non zero return value.Which of course doesn't cause any problem here but would definitely cause great problems in future when you write some important code.
So better correct it now.

well for getchar() coz sometimes i use dev when testing a program its a habbit of me to put return getchar(); rather than writing small thing since its a program made for learning

well for getchar() coz sometimes i use dev when testing a program its a habbit of me to put return getchar(); rather than writing small thing since its a program made for learning

Too lazy to just split them up like this?

getchar();
return 0;

It's better to return a fixed value when your program has exited successfully because in your current program, the return value depends on what key the user has pressed, not a very good habit IMO.
If you just write it like: return getchar(); , how can you know then whether your program has exited successfully or not?

how can i chk if it exited successfully is there a way to chk program return value ?

how can i chk if it exited successfully is there a way to chk program return value ?

The return code of your main program goes to shell/command intrepreter.
In UNIX systems the return code of the last executed command stored in a shell variable "$?" .
Ateer the excution of your program do "echo $?" to see the return code.

i have attached my code O/P logs below:

sdetlab01:/export/home/dpani/cprograms/mdump> cat ret_test.c
#include<stdio.h>

int main()
{
printf("deepak\n");

return 5;
}
sdetlab01:/export/home/dpani/cprograms/mdump> gcc -o ret ret_test.c -m64
sdetlab01:/export/home/dpani/cprograms/mdump> ret
deepak
sdetlab01:/export/home/dpani/cprograms/mdump> echo $?
5
sdetlab01:/export/home/dpani/cprograms/mdump>

MrNoob,
at post #7

yah i know now but what if i wanna count like row by row like {1,3,4} {3,5,6} it will count 1 + 3 and 3+5 and 6 + 4 ?
should i switch variables x first then i 2nd ?

Here is two dim array,

int A[2][3]={{1,3,4},
                       {3,5,6}};

x and i are two variables. I presume that a value of variables x and i:

x=0;
  i=0;
  printf("\n%d",A[i][x]); /*  value  - 1*/
  printf("\n%d",A[x][i]); /*  value  - 1*/

When you declare an array it uses contiguous memory.
Here is a code for your reference.

#include <stdio.h>
int main()
{
    int A[2][3]= {
    {1,2,3},{40,50,60}
    };
    int i;
    int *p;
    //Address calculation
    // Location of A(i) = Address of first word allocated + ( No of word allocated for each element * (i -1) )
    p=A;
    printf("\n%d at %d", *(p+0), p + 0);
    printf("\n%d at %d", *(p+1), p + 1);
    printf("\n%d at %d", *(p+2), p + 2);
    printf("\n%d at %d", *(p+3), p + 3);
    printf("\n%d at %d", *(p+4), p + 4);
    printf("\n%d at %d", *(p+5), p + 5);

    printf("\n1st element at 1st row : %d  %d",A[0][0],&A[0][0]);
    printf("\n1st element at 1st row : %d  %d",*(*(A+0)+0),*(A+0));

    printf("\n1st element at 2nd row : %d  %d",A[1][0],&A[1][0]);
    printf("\n1st element at 2nd row : %d  %d",*(*(A+1)+0),*(A+1));
    return 0;
}

I know a way to get the return value in a command line window on Windows.

First compile this program, and run it manually from the command line:

#include <stdio.h>

int main(void)
{
    int retval;

    printf("%s\n", "Enter desired return code: ");
    scanf("%d", &retval);

    return retval;
}

Do what the program asks, and when the program returns you enter the following command: echo %errorlevel% and you'll see the return value you entered :)

yah i know now but what if i wanna count like row by row like
{1,3,4} {3,5,6} it will count 1 + 3 and 3+5 and 6 + 4 ?
should i switch variables x first then i 2nd ?

Hello MrNoob,
Look at my example cleary i hope u can get the thing now.

A)a[2][3] means 2 array elements which contain 3 values each.
e.g: {a,b,c } {d,e,f}
B)a[2][3][4] : 2 array elements of type a[3][4]
so 1st break:
a[3][4] example:
{a,b,c,d},{a,b,c,d},{a,b,c,d}
c)so a[2][3][4] means 2 elements of kind a[3][4]
means
e.g
{{a,b,c,d},{a,b,c,d},{a,b,c,d} },{{a,b,c,d},{a,b,c,d},{a,b,c,d} }

D)As the way your break assgin indexes like that.and find out individual value by like
a[0][][]
a[1][][]

CONENTRATE ON THE BELOW EXAMPLE:
==================================

1)lets say
a[2][3][4]={{a,b,c,d},{a,b,c,d},{a,b,c,d} },{{a,b,c,d},{a,b,c,d},{a,b,c,d}}

2)break like:

a[0][3][4]={{a,b,c,d},{a,b,c,d},{a,b,c,d}
a[1][3][4]={{a,b,c,d},{a,b,c,d},{a,b,c,d}

3)now like this:

a[0][0][4]={a,b,c,d}
a[0][1][4]={a,b,c,d}
a[0][2][4]={a,b,c,d}

4)now like this:

a[0][0][0]='a';
a[0][0][1]='b';
a[0][0][2]='c';
a[0][0][3]='d';

Now i guess u can how to break.
RULE: break from left to right.

5)After you can clearly visualize the array you decide what u need to do then
adjust your loop variable like that.

Writing a code is not the thing,its what u understand before writing the code is important.

I know a way to get the return value in a command line window on Windows.

First compile this program, and run it manually from the command line:

#include <stdio.h>

int main(void)
{
    int retval;

    printf("%s\n", "Enter desired return code: ");
    scanf("%d", &retval);

    return retval;
}

Do what the program asks, and when the program returns you enter the following command: echo %errorlevel% and you'll see the return value you entered :)

echo %errorlevel% ---For windows Machine echo $? -----For Unix/linux Machine

You can refer to my o/p log.On the above thread.

yah thanks mate i understand now for the return value of main question i didnt search because question came up with the thread anyways thanks again guys u alawys clear up stuff i dont understand

Dear MrNoob,

If u got your things then mark the thread a solved and close it to open the forum.Because it takes time to visit solved tags again.

Hope you can understand me.

Thanks,
DP

This question has already been answered. Start a new discussion instead.