Question

Pointers is a difficult concept for many students.  I'm not sure how to make students understand the...

Pointers is a difficult concept for many students.  I'm not sure how to make students understand the concept better. In an attempt to have you try to understand pointers, I'm going to ask you to write a paper that summarizes the chapter (or an online resource) that may help you understand the concept of pointers. Include the following topics in the paper. Include illustrations either from the book, or an online source or your own illustration.

  • Come up with an introduction
  • Pointer Variable Declarations and Initialization
  • Pointer Operators
  • Pass-by-Reference with Pointers
  • Built-In Arrays
  • Using const with Pointers
  • sizeof Operator
  • Pointer Expressions and Pointer Arithmetic
  • Relationship Between Pointers and Built-In Arrays
  • Pointer-Based String
  • About Smart Pointers

You may include additional topics and illustrations to maybe help someone else understand the concept. Upload your final paper to this assignment page.

please help!!! this is programming fundamental 2 C++ and this is visual studio, thank you!

Homework Answers

Answer #1

Introduction to Pointers

Pointer is a reference

Pointer variable is a variable in which we store memory address

Pointers are used to access memory and manipulate the address.

Address: Whenever a variable is defined in any  language, a memory location is assigned for it.

Memory is a sequence of words(combination of one or more bytes), for every byte one address is assigned

Example : If var is the name of the variable, then &var will give it's address.

Pointer Variable Declarations and Initialization

Pointer Initialization is the process of assigning address of a variable to a pointer variable. Pointer variable can only contain address of a variable of the same data type.

#include<stdio.h>
    
void main()
{
    int a = 10;
    int *ptr;       //pointer declaration
    ptr = &a;       //pointer initialization
}

Pointer Operators

There are two special operators that are used with pointers * and &. The & is a unary operator that returns the memory address of its operand, for example  bal=&balance;

The second operator is *, and it is a complement of &. It is a unary operator that returns the value of the variable at the address specified by its operand. Consider the example below:

value=*balance;

Example

#include <iostream.h>
main()
{
int balance;
int *bal;
int value;

balance=3200;
bal=&balance;
value=*bal;
cout << "Balance is "<< value <<'\n';
return 0;
}

Pass-by-Reference with Pointers

When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function using the pointer is permanently made at the address of passed variable.

#include <stdio.h>
void salaryhike(int  *var, int b)
{
    *var = *var+b;
}
int main()
{
    int salary=0, bonus=0;
    printf("Enter the employee current salary:"); 
    scanf("%d", &salary);
    printf("Enter bonus:");
    scanf("%d", &bonus);
    salaryhike(&salary, bonus);
    printf("Final salary: %d", salary);
    return 0;
}

Output:

Enter the employee current salary:10000
Enter bonus:2000
Final salary: 12000

Built-In Arrays

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Using const with Pointers

A pointer to a const value is a (non-const) pointer that points to a constant value.

To declare a pointer to a const value, use the const keyword before the data type:

1

2

3

const int value = 5;

const int *ptr = &value; // this is okay, ptr is a non-const pointer that is pointing to a "const int"

*ptr = 6; // not allowed, we can't change a const value

In the above example, ptr points to a const int.

So far, so good, right? Now consider the following example:

1

2

int value = 5; // value is not constant

const int *ptr = &value; // this is still okay

A pointer to a constant variable can point to a non-constant variable (such as variable value in the example above). Think of it this way: a pointer to a constant variable treats the variable as constant when it is accessed through the pointer, regardless of whether the variable was initially defined as const or not.

Thus, the following is okay:

1

2

3

int value = 5;

const int *ptr = &value; // ptr points to a "const int"

value = 6; // the value is non-const when accessed through a non-const identifier

But the following is not:

1

2

3

int value = 5;

const int *ptr = &value; // ptr points to a "const int"

*ptr = 6; // ptr treats its value as const, so changing the value through ptr is not legal

Because a pointer to a const value is not const itself (it just points to a const value), the pointer can be redirected to point at other values:

1

2

3

4

5

int value1 = 5;

const int *ptr = &value1; // ptr points to a const int

int value2 = 6;

ptr = &value2; // okay, ptr now points at some other const int

sizeof Operator

The sizeof operator is the most common operator in C. It is a compile-time unary operator and used to compute the size of its operand. It returns the size of a variable. It can be applied to any data type, float type, pointer type variables.

#include <stdio.h>
int main() {
int a = 16;
   printf("Size of variable a : %d\n",sizeof(a));
   printf("Size of int data type : %d\n",sizeof(int));
   printf("Size of char data type : %d\n",sizeof(char));
   printf("Size of float data type : %d\n",sizeof(float));
   printf("Size of double data type : %d\n",sizeof(double));
   return 0;
}

Pointer Expressions and Pointer Arithmetic

We can perform arithmetic operations to pointer variables using arithmetic operators. We can add an integer or subtract an integer using a pointer pointing to that integer variable. The given table shows the arithmetic operators that can be performed on pointer variables:

Examples:

*ptr1 + *ptr2
*ptr1 * *ptr2
*ptr1 + *ptr2 - *ptr3

We can also directly perform arithmetic expression using integers. Lets look at the example given below where p1 and p2 are pointers.

p1+10, p2-5, p1-p2+10, p1/2 

Relationship Between Pointers and Built-In Arrays

Arrays and pointers are synonymous in terms of how they use to access memory. But, the important difference between them is that, a pointer variable can take different addresses as value whereas, in case of array it is fixed. In C , name of the array always points to the first element of an array.

An array is a block of sequential data. Let's write a program to print addresses of array elements.

#include <stdio.h>
int main() {
   int x[4];
   int i;

   for(i = 0; i < 4; ++i) {
      printf("&x[%d] = %p\n", i, &x[i]);
   }

   printf("Address of array x: %p", x);

   return 0;
}

Output

&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448

There is a difference of 4 bytes between two consecutive elements of array x. It is because the size of int is 4 bytes (on our compiler).

Notice that, the address of &x[0] and x is the same. It's because the variable name x points to the first element of the array.

From the above example, it is clear that &x[0] is equivalent to x. And, x[0] is equivalent to *x.

Another example

#include <stdio.h>
int main() {
  int i, x[6], sum = 0;
  printf("Enter 6 numbers: ");
  for(i = 0; i < 6; ++i) {
  // Equivalent to scanf("%d", &x[i]);
      scanf("%d", x+i);

  // Equivalent to sum += x[i]
      sum += *(x+i);
  }
  printf("Sum = %d", sum);
  return 0;
}

When you run the program, the output will be:

Enter 6 numbers:  2
 3
 4
 4
 12
 4
Sum = 29 

Pointer-Based String

A pointer-based string in C is an array of characters ending in the null character (''), which marks where the string terminates in memory. A string is accessed via a pointer to its first character. ... In this sense, strings are like arrays, because an array name is also a pointer to its first element.

A string literal may be used as an initializer in the declaration of either a character array or a variable of type char *. The declarations

char color[] = "blue";
const char *colorPtr = "blue";

each initialize a variable to the string "blue".

For example, here is a string:

char label[] = "Single";

What this array looks like in memory is the following:

------------------------------
| S | i | n | g | l | e | \0 |
------------------------------

where the beginning of the array is at some location in computer memory, for example, location 1000.

About Smart Pointers

A smart pointer is a wrapper class over a pointer with an operator like * and -> overloaded. The objects of smart pointer class look like a pointer but can do many things that a normal pointer can't like automatic destruction (yes, we don't have to explicitly use delete), reference counting and more.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT