NVIDIA Interview Question for Software Engineer / Developers






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

#include <stdio.h>

#define offset(a,b) (&((a *)0)->b)

struct abc {
    int a;
    int b;
    char c;
    char d;
};

int main(void) {
    printf("offset: %p\n", offset(struct abc, d));

    return 0;
}

- dan January 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can work like this also.

#include <stdio.h>

#define offset(a,b) (&((a *)NULL)->b)

struct abc { int a; int b; char c; char d;};

int main(void) { printf("offset: %p\n", offset(struct abc, d));

return 0;}

- Shyam July 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define offset(abc,c) ((char*)(&(abc.c)) - (char*)(&(abc)))

- Girish September 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

do you this is a question for dummies ?
abc is not an *object* here

it should be:
#define offset (abc, c) (char *)(&((abc *)0->c))

- asm December 01, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think your solution may be incomplete...
it seems to me should be:
#define offset(abc,c) ((char*)&(((abc*)0)->c)-(char*)(abc*)0)

- Yimin November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This won't work, the solution by dan works

- Paul March 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define offsetof(s,m) (size_t)&(((s *)0)->m) - (size_t)&(((s *)0))

((s *)0) takes the integer zero and casts it as a pointer to s.
((s *)0)->m dereferences that pointer to point to structure member m.
&(((s *)0)->m) computes the address of m.
(size_t)&(((s *)0)->m) casts the result to an appropriate data type.
By definition, the structure itself resides at address 0. It follows that the address of the field pointed to (Step 3 above) must be the offset, in bytes, from the start of the structure. At this point, we can make several observation

- focker123 March 19, 2013 | 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