Question

In: Computer Science

Discuss what we mean by pointer arithmetic and give some examples. Discuss the relationship between passing...

Discuss what we mean by pointer arithmetic and give some examples.

Discuss the relationship between passing arrays to a function and using pointers to pass an array to a function.

Solutions

Expert Solution

a)

A pointer is a address in which a numeric value. Thusly, you can perform operations on a pointer similarly as you can on a numeric value. There are four arithmetic operators that can be utilized on pointers: ++, - , +, and -

ie, ptr++, ptr--, so on.

The pointer number-crunching or arithmetic is performed comparative with the base kind of the pointer. For instance, in the event that we have a number pointer IP which contains address 1000, at that point on incrementing it by 1, we will get 1004 (1000 + 1 * 4) rather than 1001 in light of the fact that the size of the int information type is 4 bytes. On the off chance that we had been utilizing a framework where the size of int is 2 bytes then we would get 1002 ( 1000 + 1 * 2 ).

b)

We can say that  passing arrays to a function as using a call by value and  using pointers to pass an array to a function as a call by reference.

call by value, the formal parameter or argument is replicated or dupication of the actual parameters.

that is, any change is done in the formal parameter doesnot affect actual parameter.

note that actual parameter are the argument used in the calling function and formal argument are the argument used in the called function. for better understanding for actual and formal argument, see the following example below.

In the call by reference, that is using pointer the formal parameter is the reference of the actual parameter that is any change is done in formal parameter can change the actual value.

Example for passing arrays to a function using call by value

#include <stdio.h>
void display( char chr) // chr is the formal argument // called function
{
printf("%c ", chr);
}
int main()
{
char ar[] = {'h', 'e', 'l', 'l', 'o'};
for (int i=0; i<5; i++)
{
  
display (ar[i]); // ar[] is the actual argument // calling function
}

return 0;
}

Example for using pointers to pass an array to a function

#include <stdio.h>
void display( int *number) // number is the formal argument // called function
{
printf("%d ", *number);
}

int main()
{
int ar[] = {1, 2, 3, 4, 5};
for (int i=0; i<5; i++)
{
  
display (&ar[i]); // ar[] is the actual argument // calling function
}

return 0;
}


Related Solutions

Purpose Review and reinforcement of pointers, dynamic memory allocation, pointer arithmetic, passing pointers to a function,...
Purpose Review and reinforcement of pointers, dynamic memory allocation, pointer arithmetic, passing pointers to a function, returning a pointer by a function, dangling pointer, and memory deallocation, pointer initialization, and struct data type. Project description In this project, you will create a database of employees of an organization while meeting the requirements described below. Your program MUST NOT interact with the user to receive inputs, so that the instructor and/or the teaching assistant can save a big time in testing...
Briefly discuss the foundations of relationship between inflation, unemployment and GDP growth. Give examples at country...
Briefly discuss the foundations of relationship between inflation, unemployment and GDP growth. Give examples at country level. () *Use diagrams where relevant.
What we mean by Intellectual Property? What are types of intellectual property? Give examples.
What we mean by Intellectual Property? What are types of intellectual property? Give examples.
What do we mean by a risk? What are some examples of risks companies face? •...
What do we mean by a risk? What are some examples of risks companies face? • What are internal controls? What is the relationship between an internal control and a risk? Be able to identify internal controls that can be used to address specific risks.
• What does it mean when we say that there is a relationship between two variables?...
• What does it mean when we say that there is a relationship between two variables? • What kinds of relationships can there be between two variables? • Give an example of two variables that are related. For example, my daughter has an hourly salary. Her paycheck amount is related to how many hours she worked. • Give an example of two variables that are NOT related. • Why might the regression equation you have found NOT be a good...
what is the difference between hardsphere and realistic potentials also give some examples?
what is the difference between hardsphere and realistic potentials also give some examples?
3. What is an annuity? Give some examples of annuities. Distinguish between an annuity and perpetuity.
3. What is an annuity? Give some examples of annuities. Distinguish between an annuity and perpetuity.
What we mean by Intellectual Property? What are types of intellectual property? Give examples. Show how...
What we mean by Intellectual Property? What are types of intellectual property? Give examples. Show how the new technologies and internet infringing the copyright? Explain what ‘Digital Rights Management (DRM)’ is and show how it utilized to protect the copyright.
Show what relation exists between arithmetic mean, A, Geometric mean, G and Harmonic mean, H?
Show what relation exists between arithmetic mean, A, Geometric mean, G and Harmonic mean, H?
Pointer Tasks This part of the assignment will give you a chance to perform some simple...
Pointer Tasks This part of the assignment will give you a chance to perform some simple tasks with pointers. The instructions below are a sequence of tasks that are only loosely related to each other. Start the assignment by creating a file named pointerTasks.cpp with an empty main function, then add statements in main() to accomplish each of the tasks listed below. Some of the tasks will only require a single C++ statement, others will require more than one. For...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT