Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help regarding these two C programs

Status
Not open for further replies.

thecall

Junior Member level 2
Joined
Mar 4, 2007
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,448
what will b output of these two programs when icompile theses programs the output is beyond my understanding kindly tll what w will be output and why
THnx if u help


#include<stdio.h>
#include<conio.h>

main()
{
clrscr();

int n[3][3]={
2,4,3,
6,8,5,
3,5,1

};
printf("\n %d %d %d",*n,n[3][3],n[22][2]);

}

----------------------------------------------------------



#include<stdio.h>
#include<conio.h>

main()
{
clrscr();

int n[3][3]={
2,4,3,
6,8,5,
3,5,1

};
int i,*ptr;
ptr=n[0];
for(i=0;i<=8;i++)
{
printf("\n%d",*(ptr+1));
}
getch();
}
 

thecall said:
what will b output of these two programs when icompile theses programs the output is beyond my understanding kindly tll what w will be output and why
THnx if u help


#include<stdio.h>
#include<conio.h>

main()
{
clrscr();

int n[3][3]={
2,4,3,
6,8,5,
3,5,1

};
printf("\n %d %d %d",*n,n[3][3],n[22][2]);

}

}

thecall,


1) well, first of all the structure of the program is wrong and you wont be able to run it.I dont know how you have run it.
you cannot put the clrscr(); statement before the integer declaration.

2) *n?? i didnt understand what was you intention with that pointer.That will give junk values.

3) n[3][3] : this too will give you junk values since your array is a 3,3 and you given the datas for 2,2 (remember array starts from 0)

with your provided data you can print only till n[2][2] and that data is "1"
the array is built as:

3 5 1
6 8 5
2 4 3

where,
n[0][0] = 2
- - - - - - -
- - - - - - -
n[1][1] = 8
n[2][2] = 1

4) n[22][2] ??? are the errors you get out of these not enough?

are you clear?
if not,let me know.else will go to the next one.


-cheers
 

hi

thnx for helping .... well clrscr(); works for me even if it is before int declaration and the prob with *n i dont know whats the prob when i run it prints -28 which i think is a junk value but i was thinkng it as *n refers to the first element of the array isnt it.. please explain




and for n[3][3] what i know is that it means 3 rows and 3 columns in n[3][3] means 3rd element of 3rd array then why it doesnt print last value please help in this regard...........thnx in advance
 

thecall said:
*n refers to the first element of the array isnt it.. please explain

No. '*' is something to do with the address.Here probably you would have refered to teh base address of teh array?.is so,then that is not the syntax.

thecall said:
and for n[3][3] what i know is that it means 3 rows and 3 columns in n[3][3] means 3rd element of 3rd array then why it doesnt print last value please help in this regard...........thnx in advance

Pls read my previous post carefully. The first element is not n[1] .It starts from n[0].
So, in your case the last element value will be at n[2].
 

The Second program will just print the array element.

For the First program

*n is still the address of the array n
**n will be the first element of array

n[3][3] and n[22][2] will have arbitrary values that can not be known apriory, but the program will get compiled and run.
 

thnx but *n is till a doubt in my mind if it gets the ase address then doesnt it meand tthe at base address there will be first element of the array


i read *n as value pointing at n is it not correct
 

thecall,

thecall said:
thnx but *n is till a doubt in my mind if it gets the ase address then doesnt it meand tthe at base address there will be first element of the array

understand this,

Printing the base address is different and
Printing the value at the base address is different.

thecall said:
i read *n as value pointing at n is it not correct

*n will point at the first value of the array n, as you said.Thats is true for SINGLE DIMENSIONAL ARRAY.
eg:-

int n[10] = {1,2,3,4,5,6,7,8,9,10,11};
printf("%d",*n);

will print "1"
got it??

And in your first question,it is a MULTI DIMENSIONAL ARRAY.So to print the first element of the array n,you need to put two stars ,"**n".
here *n cannot show your first element.

And if you want to know the base ADDRESS OF THE ARRAY, print "n", and its data type specifier as "%u"-unsigned int.
eg:-
printf("%u",n); will give you the base address of the array.

are you clear.
 

thnx for the help quite clear now
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top