Structures
- What will be the output of the following C code?
#include <stdio.h>
struct Company
{
int c1;
char c2;
};
int main()
{
struct Company comp1[] = {10, 91, 30, 90, 52, 95};
struct Company *ptr1 = comp1;
int c1 = (sizeof(comp1) / 3);
if (c1 == sizeof(int) + sizeof(char))
printf("%d\n", ptr1->c1);
else
printf("Interview Mania");
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Interview Mania
- Which of the following structure declaration is correct ?
-
View Hint View Answer Discuss in Forum
In B semicolon is missing after the declaration. In C semicolon is missing after each structure element.
Correct Option: A
In B semicolon is missing after the declaration. In C semicolon is missing after each structure element.
- What will be the output of the following C code?
#include <stdio.h>
struct price
{
int s1;
char s2;
};
typedef struct price* ptr*;
int main()
{
struct price pr[] = {15, 52, 35, 95, 55, 56};
ptr ptr1 = pr;
printf("%d\n", ptr1->s1);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
Compilation Error
main.c:7:30: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
typedef struct price* ptr*;
^
main.c: In function ‘main’:
main.c:11:9: error: unknown type name ‘ptr’; did you mean ‘putc’?
ptr ptr1 = pr;
^~~
putc
main.c:11:20: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
ptr ptr1 = pr;
^~
main.c:12:28: error: invalid type argument of ‘->’ (have ‘int’)
printf("%d\n", ptr1->s1);
^~
- What will be the output of the following C code?
#include <stdio.h>
struct p
{
int x;
char y;
};
void foo(struct p* );
int main()
{
typedef struct p* q;
struct p p1[] = {15, 52, 43, 34, 25, 16};
foo(p1);
}
void foo(struct p* p1)
{
q ptr1 = p1;
printf("%d\n", ptr1->x);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Compilation Error
main.c: In function ‘foo’:
main.c:16:9: error: unknown type name ‘q’
q ptr1 = p1;
^
main.c:16:18: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
q ptr1 = p1;
^~
main.c:17:28: error: invalid type argument of ‘->’ (have ‘int’)
printf("%d\n", ptr1->x);
- Which of the following is an incorrect syntax for pointer to structure?
(Assuming struct test{int n;}*ptr_struct;)
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
*ptr_struct.n = 110; is an incorrect syntax for pointer to structure.