Chapter 8 -

POINTERS IN C 

Exercise


1. How is a pointer variable different from a normal variable?

(একটি পয়েন্টার ভেরিয়েবল একটি সাধারণ ভেরিয়েবল থেকে কীভাবে আলাদা?)

 Ans: pointer variables (or pointers for short) are essentially the same as other variables, which can store a piece of data. Unlike common variables that store a value (e.g., int, double, mister), the cursor stores a memory address. Cursors must be declared before they are used as normal variables.

(পয়েন্টার ভেরিয়েবল (অথবা সংক্ষেপে পয়েন্টার) মূলত অন্যান্য ভেরিয়েবলের মতোই, যা এক টুকরো ডেটা সংরক্ষণ করতে পারে। সাধারণ ভেরিয়েবলের মতো নয় যা একটি মান সংরক্ষণ করে (যেমন, int, double, mr), কার্সারগুলি একটি মেমরি ঠিকানা সংরক্ষণ করে। কার্সারগুলিকে সাধারণ ভেরিয়েবল হিসেবে ব্যবহার করার আগে ঘোষণা করতে হবে।)

2. Why is dynamic memory allocation an efficient memory management technique?

(ডাইনামিক মেমোরি অ্যালোকেশন কেন একটি দক্ষ মেমোরি ব্যবস্থাপনা কৌশল?)

Ans: Dynamic memory allocation is a computer programming technique used to manage memory more efficiently. It allows a program to allocate memory blocks, make them free, and reassign them during program editing

(ডায়নামিক মেমোরি অ্যালোকেশন হল একটি কম্পিউটার প্রোগ্রামিং কৌশল যা মেমোরিকে আরও দক্ষতার সাথে পরিচালনা করতে ব্যবহৃত হয়। এটি একটি প্রোগ্রামকে মেমোরির ব্লক বরাদ্দ করতে, সেগুলিকে মুক্ত করতে এবং প্রোগ্রাম সম্পাদনার সময় সেগুলিকে পুনরায় বরাদ্দ করতে দেয়)

3. How many bytes are needed to store an int pointer variable? Is it the same for a char pointer variable? Write a simple C program to explain your answer.

(একটি int পয়েন্টার ভেরিয়েবল সংরক্ষণ করতে কত বাইট প্রয়োজন? এটি কি char পয়েন্টার ভেরিয়েবলের জন্য একই রকম? আপনার উত্তর ব্যাখ্যা করার জন্য একটি সহজ C প্রোগ্রাম লিখুন।)

Ans: The size of the cursor variables is the same for each data type. It will be 8 bytes for the 64-bit processor, 4 bytes for the 32-bit processor, 2 bytes for the 16-bit processor.

(প্রতিটি ডেটা টাইপের জন্য কার্সার ভেরিয়েবলের আকার একই। এটি 64-বিট প্রসেসরের জন্য 8 বাইট, 32-বিট প্রসেসরের জন্য 4 বাইট, 16-বিট প্রসেসরের জন্য 2 বাইট হবে)।

4. Write the output of the following code segment.

(নিম্নলিখিত কোড সেগমেন্টের আউটপুট লিখুন।)

a.










Ans: Output was 9 


b.











Ans: Output was 9 and 10

C.











Ans: Output was 10


d. 








Ans: Output was 10


e. 







Ans: Output was A


f. 







Ans: Output was B

g. 








Ans: Output was B

5. Write a C program to dynamically allocate memory for an array to store 10 integers and display the first 5 out of them.

(একটি অ্যারের জন্য গতিশীলভাবে মেমরি বরাদ্দ করার জন্য একটি C প্রোগ্রাম লিখুন যাতে 10টি পূর্ণসংখ্যা সংরক্ষণ করা যায় এবং তাদের মধ্যে প্রথম 5টি প্রদর্শন করা যায়।)

Ans: 


6. Write a C program to dynamically allocate memory for an array to store runs scored by Virat Kohli in the last ten ODI cricket matches. Write a function to find the maximum one.

(গত দশটি ওয়ানডেতে বিরাট কোহলির করা রান সংরক্ষণের জন্য গতিশীলভাবে মেমরি বরাদ্দ করার জন্য একটি C প্রোগ্রাম লিখুন। সর্বাধিক একটি খুঁজে বের করার জন্য একটি ফাংশন লিখুন।)
Ans: 

7. Write a C program and define a function that takes the length of your name as an input parameter and then allocates memory dynamically to store your name. Write another function to display the name.

একটি সি প্রোগ্রাম লিখুন এবং এমন একটি ফাংশন সংজ্ঞায়িত করুন যা আপনার নামের দৈর্ঘ্যকে ইনপুট প্যারামিটার হিসেবে গ্রহণ করে এবং তারপর আপনার নাম সংরক্ষণের জন্য গতিশীলভাবে মেমরি বরাদ্দ করে। নামটি প্রদর্শনের জন্য আরেকটি ফাংশন লিখুন।

8. Write a C program to store some integer variables in an array. Then write functions to the following.

কিছু পূর্ণসংখ্যার ভেরিয়েবল একটিতে সংরক্ষণ করার জন্য একটি সি প্রোগ্রাম লিখুন। তারপর ফাংশনগুলো নিচের দিকে লিখুন।

1. To calculate the number of even numbers in the array.

(একটি অ্যারেতে সমান সংখ্যার সংখ্যা গণনা করতে।)

Ans: 

2. To dynamically allocate memory to a new array to store only the even numbers.

শুধুমাত্র সমান সংখ্যা সংরক্ষণের জন্য একটি নতুন অ্যারেতে গতিশীলভাবে মেমরি বরাদ্দ করা।

3. To copy the even numbers from the first array to the second one.

প্রথম অ্যারে থেকে দ্বিতীয় অ্যারেতে সমান সংখ্যার প্রতিলিপি তৈরি করা।

Sample run:


First array


Second array




9. Write a C program to store some integer variables in an array. Then write functions to the following.

একটি অ্যারেতে কিছু পূর্ণসংখ্যার ভেরিয়েবল সংরক্ষণ করার জন্য একটি C প্রোগ্রাম লিখুন। তারপর ফাংশনগুলো নিচের দিকে লিখুন।

1. To calculate the number of non-zero elements that are divisible by 3.

৩- দ্বারা বিভাজ্য নয় এমন অ-শূন্য উপাদানের সংখ্যা গণনা করা।

2. To dynamically allocate memory to a new array to store only those elements.

 শুধুমাত্র সেই উপাদানগুলি সংরক্ষণ করার জন্য একটি নতুন অ্যারেতে গতিশীলভাবে মেমরি বরাদ্দ করা।

3. To copy the selected elements from the first array to the second one.

প্রথম অ্যারে থেকে দ্বিতীয় অ্যারেতে নির্বাচিত উপাদানগুলির প্রতিলিপি তৈরি করা।

4. To calculate the summation of these elements.

 এই উপাদানগুলির সারাংশ গণনা করা।

Sample run:


First array


Second array