Showing posts with label cinterviews question and answers. Show all posts
Showing posts with label cinterviews question and answers. Show all posts

How many levels of pointers can you have?

How many levels of pointers can you have?

Question :How many levels of pointers can you have?

Answer: The answer depends on what you mean by “levels of pointers.” If you mean “How many levels of indirection can you have in a single declaration?” the answer is “At least 12.”

int i = 0;
int *ip01 = & i;
int **ip02 = & ip01;
int ***ip03 = & ip02;
int ****ip04 = & ip03;
int *****ip05 = & ip04;
int ******ip06 = & ip05;
int *******ip07 = & ip06;
int ********ip08 = & ip07;
int *********ip09 = & ip08;
int **********ip10 = & ip09;
int ***********ip11 = & ip10;
int ************ip12 = & ip11;
************ip12 = 1; /* i = 1 */

The ANSI C standard says all compilers must handle at least 12 levels. Your compiler might support more.
Keywords:
how many levels of indirection in pointers can you have in a single declaration?
when should we use pointers in a c program?
how to call a function without using the function name to send parameters
what is null pointer

Difference between const char* p and char const* p?

Difference between const char* p and char const* p?

In const char* p, the character pointed by ‘p’ is constant, so u cant change the value of character pointed by p but u can make ‘p’ refer to some other location. in char const* p,

the ptr ‘p’ is constant not the character referenced by it, so u cant make ‘p’ to reference to any other location but u can change the value of the char pointed by ‘p’.

Keywords:
const char
const char* string
difference between char *p and char *p
const pointer
difference between const char and string
pointer to const char array
static const char in c
const char* in cpp

How to check whether a linked list is circular?

 How to check whether a linked list is circular?

Create two pointers, and set both to the start of the list. Update them as increment first pointer by one and second pointer by two if its a circular link list they will defenitely meet at some point

Below is a sample program to check circular link list:-
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next;
if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print ("circular");
}}
Keywords:
circular linked list
circular linked list java
check if linked list is palindrome
circular linked list with one node
floyd's cycle-finding algorithm c++
circular linked list in c
reverse a linked list
circular linked list in c++

What is the output of printf("%d")?

What is the output of printf("%d")?

printf() Function
What is the output of printf("%d")?

1. When we write printf("%d",x); this means compiler will print the
value of x. But as here, there is nothing after %d so compiler will show
in output window garbage value.
2. When we use %d the compiler internally uses it to access the
argument in the stack (argument stack). Ideally compiler determines
the offset of the data variable depending on the format specification
string. Now when we write printf("%d",a) then compiler first accesses
the top most element in the argument stack of the printf which is %d
and depending on the format string it calculated to offset to the actual
data variable in the memory which is to be printed. Now when only %d
will be present in the printf then compiler will calculate the correct
offset (which will be the offset to access the integer variable) but as
the actual data object is to be printed is not present at that memory
location so it will print what ever will be the contents of that memory
location.
3. Some compilers check the format string and will generate an error
without the proper number and type of arguments for things like
printf(...) and scanf(...).

Keywords:
c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference
cinterviews.com
printf(%d main)
printf(%d 1)
online c compiler
output of printf d
how printf and scanf works
return type of printf() in c