Pointer to single dimensional numeric array

Array का नाम box zero का constant address है: वह ख़ुद हिल नहीं सकता, पर उस address की copy रखने वाला pointer हिल सकता है।

5 min read · 9 cards · 5 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

कौन-सी line ठुकराई जाएगी?

Riya का जोड़ने वाला loop, array के नाम को ही आगे बढ़ाता है:

int marks[5] = {70, 55, 90, 62, 78}, total = 0;

total += *marks;

marks++;

कौन-सी line compile नहीं होगी?

Think first

पहले आपका अंदाज़ा

Line और अपनी वजह type कीजिए।

Show the answer

marks++। Array का नाम एक constant address है: box zero पर गड़ा एक signpost, और signposts हिलते नहीं। *marks ठीक है: उसका मतलब है marks[0], यानी 70। Address को p में copy कर लीजिए: p++ legal है।

Theory

नाम यानी address

marks लिखने पर &marks[0] मिलता है, यानी base address, इसलिए int *p = &marks[0]; छोटा होकर int *p = marks; बन जाता है, & के बिना। C, marks[i] को *(marks + i) के रूप में define करता है (i boxes आगे चलिए, फिर box खोलिए), इसलिए *(p + i) और p[i] भी उसी box तक पहुँचते हैं।

Think first

कौन-से 90 देते हैं?

p = marks हो, तो इनकी value निकालिए: marks[2], *(marks + 2), *(p + 2), p[2], *p + 2।

Show the answer

शुरू के 4 expressions, box 2 को पढ़ते हैं: 90। *p + 2 72 है: * पहले 70 पढ़ता है, फिर उसमें 2 जोड़ा जाता है।

Quiz

जब name एक char array हो, तब scanf("%s", name) को & क्यों नहीं चाहिए?

  1. name पहले से ही name[0] का address है
  2. %s की वजह से scanf ख़ुद & जोड़ लेता है
  3. Strings, scanf को address की जगह value के रूप में मिलती हैं
  4. Compilers छूटे हुए & को चुपचाप माफ़ कर देते हैं
Show the answer

name पहले से ही name[0] का address है

name का मतलब पहले से ही &name[0] है, यानी वह जगह जहाँ scanf को लिखना है; एक साधारण int का नाम सिर्फ़ उसकी value देता है। न कुछ जोड़ा जाता है, न कुछ माफ़ होता है।

Think first

Size बराबर?

p और marks दोनों &marks[0] देते हैं। क्या sizeof(p) और sizeof(marks) बराबर हैं?

Show the answer

नहीं। sizeof(marks) पूरे array का size है: int 4 bytes का हो तो 5 × 4 = 20 bytes; sizeof(p) सिर्फ़ एक pointer का size है, आम तौर पर 4 या 8 bytes। Functions को marks सिर्फ़ एक address के रूप में मिलता है (pass by reference, Unit 4)।

Practical

Exam-style: output बताइए

#include <stdio.h>

int main() {
    int marks[5] = {70, 55, 90, 62, 78};
    int *p = marks;
    int i, total = 0;

    for (i = 0; i < 3; i++) {
        total += *p;
        p++;
    }
    printf("%d %d", total, p[1]);
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

Exam-style: ऊपर वाले program का output क्या है?

  1. 215 78
  2. 215 62
  3. 215 55
  4. यह compile नहीं होता
Show the answer

215 78

Pointer पर p++ legal है। 3 rounds में 70 + 55 + 90 = 215 जुड़ते हैं और p, marks[3] पर पहुँच जाता है; p[1] वहीं से गिनता है: marks[4], यानी 78। आख़िरी p++ भूल जाएँ तो 62 आता है; p[1] को marks[0] से गिनें तो 55 आता है।

Summary

Key takeaways

  • marks लिखने पर &marks[0] मिलता है, इसलिए int *p = marks; को & नहीं चाहिए।
  • marks[i] यानी *(marks + i); p[i] यानी *(p + i), गिनती वहाँ से जहाँ p खड़ा है।
  • p++ legal है, marks++ नहीं; sizeof(marks) पूरे array का size है।
  • याद रखने का hook: नाम एक गड़ा हुआ signpost है, और signposts हिलते नहीं।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of Arrays and Pointer

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Pointer to single dimensional numeric array · Computer Programming and Programming Methodology (CPPM) · Gri-Learn