What is the purpose of the gate2011 Program in the C programming language?

  • Thread starter Thread starter sam topper.
  • Start date Start date
  • Tags Tags
    Explain Program
AI Thread Summary
The discussion revolves around a C program that manipulates pointers and arrays to print a substring from a string. The code initializes a character array with "gate2011" and uses pointer arithmetic to calculate the starting point of the substring. Specifically, it evaluates the expression `p + p[3] - p[1]`, which effectively computes the address of the character '2' in the string. The calculation results in a pointer that points to the substring "2011". The conversation highlights the equivalence of pointers and arrays in C, noting that both `&a[4]` and `*(a + 4)` access the fifth element of an array. An alternative method to achieve the same output is also presented, simplifying the code by directly using the original array without redefining the pointer. Overall, the discussion emphasizes understanding pointer arithmetic and string manipulation in C programming.
sam topper.
Messages
14
Reaction score
0
#include<stdio.h>
void main()
{
char c[]="gate2011";
char *p=c;
printf("%s",p+p[3]-p[1]);
}
 
Technology news on Phys.org
Doesn't look like rocket science. What do you think will happen? What actually happens when you run it?
 
It apparently takes the adress of the string + 65-61= adr + 4. Now I don't know but this seems not aligned, but it means maybe adr + 4 bytes, hence it maybe prints '2011', but I have not checked this for real.
 
Last edited:
The trick is the way that pointers and arrays are equivalent in C++.
If you have an array
Code:
int[] a;
then a is a pointer to an int, and &a[4] and *(a + 4) both point to address of the 5th array element.
So the pointer p points at the beginning of the string, p[4] - p[1] is the length of the memory block used by the first four characters and the sum is a pointer to the place in memory where the '2' is stored. Since strings are terminated by a null (\0) the statement is equivalent to
Code:
    char p[] = { '2', '0', '1', '1', '\0' };
    printf("%s", p);

You could actually reach the same thing through
Code:
#include<stdio.h>
void main()
{
    char c[]="gate2011";
    // No need to introduce a new name, c is already a char*
    // char* p = c; 
    printf("%s", (c + 4));
}
 
  • Like
Likes 1 person
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top