HCL Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
5
of 5 vote

You cannot dereference a void pointer. Cast p in the printf statement to types int and float.

- Shiva August 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we can't dereference a void pointer

- kaushik August 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 vote

do typecast of void pointer p to the type of which you want to print value, stored in void pointer. eg, if you want to print int value of binary combination represented by void pointer p, then do typecast like, *(int *)p.

#include<stdio.h>
  void main()
  {
          void *p;
          int x=10;
          float y=20.2;
          p=&x;
          printf("int %d\n",*(int *)p);
         // *(int *)p=20; // useless line
          p=&y;
          printf("float %g\n",*(float *)p);
 }

- P.S. Patel August 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Also, I guess you could do this with nonbasic data types (classes).

class T {
public 
	int a;
};

int main()
{
T t;
void* p = &t;
t.a = 0;
printf("%d \n", ((T*) p)->a);
}

- Ehsan August 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Cannot dereference the void pointer in printf's
To print you have to use this way,
printf("int %d\n",*(int *)p);
Also for assinging have to assing in this format,
*(float *)p=30.5;
And then print as
printf("float %f\n",*(float *)p);

Note format specifier %f is used, %d will print wrong value.

- nikhil.kathare September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
  void main()
  {
          int *p;
          float *t;
          int x=10;
          float y=20.2;
          p=&x;
          printf("int %d\n",*p);
          *p=20;
          t=&y;
          printf("float %f\n",*t);
 }

- xiang.zh August 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int 10
float 20.2

- sudharsan September 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we cant dereference the void pointer..........if ur code shud run proper use typecast before u print the *p value

- chandu December 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

should be:

*p=(float)20;

- Anonymous August 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

printf("%d",*(int *) p);
printf("%f",*(float *)p);

- santhosh reddy September 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
int main()
{
void *p;
int x=10;
float y=20.2;
p=&x;
printf("int %d\n",*((int*)p));
*((int*)p)=20;
p=&y;
printf("float %f\n",*((float*)p));
getch();
return 0;

}

- mithilesh kumar (tict kolkata) November 07, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More