One dimensional array of numbers and characters: declaration, initialization

Array એ એક જ નામ છે એકસરખા-type વાળા boxes ની એક હરોળનું, જે ZERO થી numbered છે: int marks[60], 60 variables ની જગ્યા લે છે, અને અંતે એક છુપા '\0' વાળો char array એ જ C માં text રાખવાની રીત છે.

11 min read · 10 cards · 3 checks

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


Theory

60 variables, કે એક idea

Marks program ને બધા 60 students ના marks એકસાથે યાદ રાખવાના છે (તમે topper ત્યારે શોધી ન શકો જ્યારે દરેક mark ને આગળનો આવતાં જ ભૂલતા જાઓ).

Horror-movie વાળું version: int m1, m2, m3, ... m60; અને 60 scanf lines.

C વાળું version: એક નામ, 60 numbered boxes:

int marks[60];

એ એક array છે, અને loops પછી, એ આ subject નો બીજો સૌથી મોટો idea છે. એ C ની સૌથી પ્રખ્યાત નવાઈ પણ છુપાવે છે: numbering ZERO થી શરૂ થાય છે.

Theory

એક railway coach

Array એ railway coach છે: એક coach નું નામ (S4), ઘણી એકસરખી berths, દરેક પર એક number. તમે ક્યારેય નથી કહેતા "Ramesh નામની berth"; તમે કહો છો "S4, berth 23". એકસરખા આકારની berths (એક જ data type), સળંગ (memory માં અડોઅડ), number થી તરત પહોંચમાં. C ની એક વિચિત્ર વાત: પહેલી berth નો number 0 છે, અને આ unit નો દરેક આગળનો lesson આ જ વાત પર ટકેલો છે.

Theory

Declaration અને initialization

  • Declare: int marks[5]; પાંચ int boxes reserve કરે છે, array ના આયખા સુધી size નક્કી.
  • Initialize: int marks[5] = {70, 55, 90, 62, 78};
  • Partial: int marks[5] = {70, 55}; બાકીને zeros થી ભરે છે.
  • C ને ગણવા દો: int marks[] = {70, 55, 90}; એક size-3 array બનાવે છે.

Indexing: marks[0] પહેલો element છે, marks[4] પાંચમાંનો છેલ્લો. Index હંમેશા 0 થી n-1 સુધી ચાલે છે. અને marks[i] એક for loop સાથે એ જ લગ્ન છે જેના વિશે આ unit છે:

for (i = 0; i < 5; i++) printf("%d\n", marks[i]);

Theory

Character arrays: C માં text

Text બસ chars નું એક array છે:

char name[10] = "Riya";

Memory રાખે છે: R i y a `\0` અને પાંચ ખાલી boxes. એ \0 (null terminator, ASCII 0) invisible છે પણ જરૂરી: એ ચિહ્નિત કરે છે કે TEXT ક્યાં પૂરું થાય છે, કારણ કે array પોતે word કરતાં મોટું છે. દરેક string function જેને તમે મળશો (strlen, printf %s...) array પર ત્યાં સુધી ચાલે છે જ્યાં સુધી \0 ન મળે.

ધ્યાનથી ગણો: "Riya" ને ઓછામાં ઓછા 5 boxes જોઈએ, ચાર letters અને terminator.

Quiz

int marks[5] માટે, કયા indexes valid છે?

  1. 0 to 4
  2. 1 to 5
  3. 0 to 5
  4. 1 to 4
Show the answer

0 to 4

પાંચ boxes, zero થી numbered: 0, 1, 2, 3, 4. marks[5] લખવું array ની બહાર પગલું મૂકે છે, અને C તમને રોકતું નથી: એ compile થાય છે, ચાલે છે, અને છાનેમાને જે પડોશમાં રહે છે એને corrupt કરી દે છે (undefined behaviour). n-boxes-નો-અર્થ-0-થી-n-1 વાળો નિયમ અસ્તિત્વમાં સૌથી વધુ test થતું array તથ્ય છે.

Think first

Boxes વાંચો

int marks[5] = {70, 55, 90, 62, 78}; આપ્યું છે, મનમાં કાઢો: marks[0], marks[2], અને marks[4] શું છે? અને marks[5] માં શું ખતરનાક છે?

Show the answer

marks[0] = 70 (પહેલો box), marks[2] = 90 (ત્રીજો box), marks[4] = 78 (પાંચમો અને છેલ્લો). marks[5] એ પાંચ-box array નો છઠ્ઠો box હશે: out of bounds, કોઈ compiler error નહીં, runtime પર garbage વાંચવું કે પડોશીને corrupt કરતું લખવું. જો તમે marks[2] માટે 55 જવાબ આપ્યો, તો તમે એક થી ગણ્યું, બરાબર એ જ આદત જેને આ unit એ તોડવાની છે.

Quiz

char city[10] = "Surat"; Text ખરેખર કેટલા boxes રોકે છે, અને letter t ના બરાબર પછી શું બેસે છે?

  1. 6 boxes: S-u-r-a-t અને terminator '\0'
  2. 5 boxes: બરાબર પાંચ letters
  3. 10 boxes: આખું array જ text છે
  4. 6 boxes: પાંચ letters અને એક space character
Show the answer

6 boxes: S-u-r-a-t અને terminator '\0'

String literals એક invisible મહેમાન લાવે છે: છેલ્લા letter પછી null terminator '\0', એટલે "Surat" દસ-box array ની અંદર છ boxes રોકે છે. બાકીના ચાર ખાલી છે. એ '\0' (ASCII 0) છે, space (ASCII 32) નહીં, અને આ +1 ને ભૂલવું એ ક્લાસિક char-array sizing error છે.

Watch out

Marks (અને programs) ક્યાં કપાય છે

એક થી ગણવું: marks[1] એ બીજો element છે; zero-start પાક્કું કરી લો. Out of bounds: valid indexes n-1 પર પૂરા; marks[n] બરાબર compile થાય છે અને છાનેમાને corrupt કરે છે, C તમારા પર પૂરો ભરોસો કરે છે. '\0' ની જગ્યા ભૂલવી: char name[4] = "Riya" માં terminator માટે જગ્યા નથી, અને string functions અંતના આગળ ભાગી જશે. Fixed size: array પછી વધી શકતું નથી; જેટલું વધુમાં વધુ જોઈએ એટલું declare કરો.

Theory

અહીંથી unit નો રસ્તો

પછી માટે એક વધુ રહસ્ય: array નું NAME (marks) ખરેખર box zero નો ADDRESS છે, એટલે જ scanf("%s", name) ને કોઈ & જોઈતું ન હતું, અને એ જ આગળ ત્રણ lessons પછી pointers ની આખી વાર્તા બની જાય છે. પણ આગળ: arrays ને કામમાં લેવા, કોઈ roll number શોધવો અને toppers ની list sort કરવી, એ operations જે exams દર વર્ષે પૂછે છે.

Summary

Key takeaways

  • Array: એક નામ, n એકસરખા-type boxes, memory માં સળંગ, size નક્કી.
  • Indexing ZERO થી શરૂ: valid indexes 0 થી n-1; marks[0] પહેલો છે.
  • પૂરું initialize કરો, આંશિક (બાકી 0 થઈ જાય), કે {} થી C ને ગણવા દો.
  • marks[i] + એક for loop, ડઝનબંધ variables ની જગ્યા લે છે.
  • char arrays text રાખે છે એક invisible '\0' terminator સાથે: "Riya" ને 5 boxes જોઈએ.
  • Out-of-bounds access છાનેમાને compile થાય છે અને corrupt કરે છે, C ક્યારેય તપાસતું નથી.
  • યાદ રાખવાની યુક્તિ: એક railway coach, berths zero થી numbered.

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

One dimensional array of numbers and characters: declaration, initialization · Computer Programming and Programming Methodology (CPPM) · Gri-Learn