The output of the above would be : [L] [i] Since the priority of both '++' and '*' are same so processing of '*ptr++' takes place from right to left. Going by this logic, ptr++ is evaluated first and then *ptr. So both these operations result in 'L'. Now since a post fix '++' was applied on ptr so the next printf() would print 'i'.
This is because, through *ptr = 'T', the code is trying to change the first byte of the string 'Linux' kept in the code (or the read-only) segment in the memory. This operation is invalid and hence causes a seg-fault or a crash.
Following piece of code tries to do the required : #include<stdio.h> int main(intargc, char *argv[]) { int i = 0; char buff[100]; memset(buff,0,sizeof(buff)); strncpy(buff, argv[0], sizeof(buff)); memset(argv[0],0,strlen(buff)); strncpy(argv[0], "NewName", 7); // Simulate a wait. Check the process // name at this point. for(;i<0xffffffff;i++); return 0; }
Though the above program may run perfectly fine at times but there is a serious loophole in the function 'inc()'. This function returns the address of a local variable. Since the life time of this local variable is that of the function 'inc()' so after inc() is done with its processing, using the address of its local variable can cause undesired results. This can be avoided by passing the address of variable 'a' from main() and then inside changes can be made to the value kept at this address.
The output of the above code would be : 110..40..60 This is because the arguments to the function are processed from right to left but are printed from left to right.
Scope of a variable is the part of the program where the variable may directly be accessible. In C, all identifiers are lexically (or statically) scoped. See this for more details.
There can be many ways. Following are some of them. #include<stdio.h> int main() { if (printf("Hello World") ) { } } #include<stdio.h> int main() { while(!printf("Geeks for Geeks\n")) { } }
1. To get address of a variable 2.For achieving pass by reference in C: Pointers allow different functions to share and modify their local variables. 3. To pass large structures so that complete copy of the structure can be avoided. 4. To implement "linked" data structures like linked lists and binary trees.
Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate. /* Function with memory leak */ #include <stdlib.h> void f() { int *ptr = (int *) malloc(sizeof(int)); /* Do some work */ return; /* Return without freeing ptr*/ }