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
Click For Summary

Discussion Overview

The discussion revolves around the purpose and behavior of a specific C programming code snippet involving string manipulation and pointer arithmetic. Participants explore the implications of the code, its output, and the underlying principles of pointers and arrays in C.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant suggests that the code will print '2011' based on their interpretation of pointer arithmetic, although they have not verified this.
  • Another participant explains the equivalence of pointers and arrays in C, detailing how pointer arithmetic can be used to access specific elements of a string.
  • A later reply proposes an alternative approach to achieve the same result without introducing a new pointer variable, emphasizing the direct use of the original string array.

Areas of Agreement / Disagreement

Participants express differing interpretations of the code's behavior, with some uncertainty about the exact output and the alignment of memory addresses. No consensus is reached on the interpretation of the pointer arithmetic involved.

Contextual Notes

There are unresolved assumptions regarding the behavior of pointer arithmetic in the context of the specific C implementation being discussed, as well as potential variations in output based on compiler behavior.

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 address 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   Reactions: 1 person

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
14
Views
4K
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
89
Views
7K
Replies
86
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
73
Views
6K
  • · Replies 20 ·
Replies
20
Views
2K