Question

1) Explain the difference between: Array of characters like char c[] = {'A', 'B', 'C'}; AND...

1)

Explain the difference between:

Array of characters like char c[] = {'A', 'B', 'C'};

AND

c-string like string = "ABC";

2)

We can have two pointers that point to one memory location such as:

int *intPtr1, *intPtr2;

intPtr1 = &x;

intPtr2 = &x;

a) What method we have to make sure only one pointer points to a variable?

b) Give an example.

3)

a) Can a pointer to a constant receive an address of a constant item and/or non-constant item?

b) Explain and give example(s).

Homework Answers

Answer #1

Here is the solution to above problem . Please read the explanation for more information

PLEASE GIVE A THUMBS UP!!!

1)

Explain the difference between:

Array of characters like char c[] = {'A', 'B', 'C'};

AND

c-string like string = "ABC";

Answer

A c character array does not contain the '\0' null character at the end hence it cannot be printed on the console like a c-string , where as the c-string "ABC" contains the '\0' null character at the end and can be printed on the console. '\0' character is used in C to tell the compiler when a c-string has ended

2)

We can have two pointers that point to one memory location such as:

int *intPtr1, *intPtr2;

intPtr1 = &x;

intPtr2 = &x;

Answer

Yes, we can have two pointers pointing to the same memory location, The task of a pointer is to store the memory address using the star operator (*) we can access the address stored inside a pointer. Hence it is perfectly alright situation where two pointers are pointing to same memory

3)

a) Can a pointer to a constant receive an address of a constant item and/or non-constant item?

b) Explain and give example(s).

Answer

Yes, A pointer to constant can store address of a non-constant variable as well, below is the code for same

   const int x=5;
   const int * p = &x;
   int y=7;
   p=&y;

This works without any issue

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
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Please create an array of Leg objects, one constructor that takes three parameters as constant C...
Please create an array of Leg objects, one constructor that takes three parameters as constant C string, and one number representing the distance in miles between the two cities Write a code block to create a static array (that is, not dynamic and not a vector) of 3 Leg objects using city names of your choosing. That's THREE objects, each created using THREE parameters. For example, the Leg class declaration looked like, class Leg { const char* const startCity; const...
In the following C code, Which variable if NOT of primitive data type? A. a B....
In the following C code, Which variable if NOT of primitive data type? A. a B. b C. c D. d int a = 10; double b = 20.0; float c = false; char d[5] = "Hello"; // here we define a string In programming language C, the implementation of a string data type is limited dynamic length, which means the length of a string variable is fixed once it has been defined. A. True B. False In C# programming...
In this lab, we want to get some practice with pointers -- specifically, the basic syntax...
In this lab, we want to get some practice with pointers -- specifically, the basic syntax of declaring them, storing memory addresses into them, dereferencing them, and seeing how they work with pointer arithmetic in the context of built-in, C-style arrays. Unless specified otherwise, you can use any valid variable names you wish. The requirements for this program are as follows: Part A Declare a pointer-to-int, initialized to nullptr. Declare an int variable, initialized to some value of your choice....
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a...
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a string, get a string from the user, open file, close file, read file, and allocate heap memory. You can use more macros than these if you like. Main Program File: Allocate 1024 bytes of dynamic memory and save the pointer to the area. The main program is a loop in which you ask the user for a filename. If they enter nothing for the...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
Translate one array operation from C to ASM: a = b * X[Y[2]], where assume X[]...
Translate one array operation from C to ASM: a = b * X[Y[2]], where assume X[] is from the memory address $s0; Y[] is from the memory address $s1; (ASM will use the least number of registers as needed)