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
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....
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)
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
Assume we have CPU instructions that look like this: load register, address save register, address Where...
Assume we have CPU instructions that look like this: load register, address save register, address Where the instruction load copies the data pointed to by the address into the register, and the instruction save copies the data from the register into the location pointed to by address. Assume “register” can be the values R0 and R1, where each “R#” is the name of a single register that can store a single byte. Assume the “address” is an integer constant number....
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT