You are not logged in. Please login at www.codechef.com to post your questions!

×

problem when we declare any structure in c and class in java? help me

in terms of java-- my class is --

class Element{

int value; Element e;}

 public static void main(String[] args){
  Element obj=new Element();  // it  wil assign 0 to obj.value and null to obj.e value by default

 }

but below in c, in similar scenarios, "struct Element obj;" it will give 0 to obj.value by default but what about obj.e=???

my structure in c is as

struct Element1{
int value;
struct Element nextAddress;

};

struct Element{
int value;
struct Element *nextAddress;

};


struct Element *head=NULL;
struct Element1 head2=NULL (error why);

 struct Element head1; //plz exaplain me what will happen when we write this line.. i know 0 will given 
 to value of head1 and 
  what will be assign to  struct nextAddress as by default head1 is  globaly declare in c     //**here 
  we cant intilize this structure to NULL (Why) but in java we can do so**
 // like suppose we have class Element than we can write as *Element e=NULL*;


void main(){
struct Element *p;
    clrscr();
printf("%d",head1.value);//its giving 0
//how to check what is inside head1.nextAddress?? plz help me
//printf("%d",head1.value);


getch();

}

asked 13 Jun '17, 12:09

hemant_dhanuka's gravatar image

5★hemant_dhanuka
533112
accept rate: 3%


Not sure about java but in c, pointers should be initialized to NULL when declared globally.

Error in the second line is because you are assigning NULL to a non-pointer type variable.

To check the value of next address, since it is a pointer, use %x and refer to it as head->nextAddress

link

answered 13 Jun '17, 13:26

abdullah768's gravatar image

6★abdullah768
2.5k421
accept rate: 17%

edited 13 Jun '17, 13:32

@abdullah768 zero is assignned when variable is declare at global and memory is assigned from data segment and garbage value assigned when variable is local and allocated from stack memory segment i know that dear... why we cant assign NULL to a non-pointer type variable? how to check what value is inside nextAddress as it struct type.. we can check int values as printf("%d", head1.value); but i want to check printf("%??",head1.nextaddress);

(13 Jun '17, 13:36) hemant_dhanuka5★

Sorry, I hadn't realized it was declared globally, edited to reflect changes.

You can use %x as pointers are hexadecimals.

(13 Jun '17, 13:37) abdullah7686★

NULL can't be stored in any other data type just like you can't store c in an int.

(13 Jun '17, 13:39) abdullah7686★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×3,828
×1,491
×1,409
×1,314
×81
×6
×3

question asked: 13 Jun '17, 12:09

question was seen: 488 times

last updated: 13 Jun '17, 13:39