Question

In: Computer Science

When you create a C string (an array of chars) without the * symbol for pointer,...

When you create a C string (an array of chars) without the * symbol for pointer, is the pointer towards the 1st char created or not along with the array?

Solutions

Expert Solution

The answer is yes, when we create a C string without * symbol, then also pointer towards the 1st char is created.

We can ellaborate this using various examples:-

Example 1:

// taking a C string

char string[10] = "string";

// if we try to print the 1st character like

printf("%c",string[0]);

// the output will be as

s

Example 2:

// taking a C string

char string[10] = "string";

// print address of 1st char

printf("%d",string);

// output

It will give an address like (-1627204058)

// explanation

Even though we have not created the string using * symbol, the name of the array(here string) holds the base address i.e. the address of the 1st charcter of the string same as a pointer which holds the base address of the array, that's why when we tried to print string[0] we got the 1st character i.e. 's' and when we tried to get address of 1st char, we got it using the name of the array i.e. 'string'. Thus it justifies that a pointer towards the 1st character of the string is always created.


Related Solutions

In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable (like ptr) which points to this array. Use this pointer variable to initialize the myNums array from 2 to 200 and then display the array elements. Delete the dynamic array myNums at the end. You just need to write part of the program.
C# - count =0; - Create a 2d array "items". string[,] items = new string[100, 4];...
C# - count =0; - Create a 2d array "items". string[,] items = new string[100, 4]; - Create a do while loop, when the user enters "0", stop the loop. - Users enter the item name, price, quantity, save this info and subtotal to array. - increase "count++" -convert price(decimal, Convert.ToDecimal() ) and quantity(int) to do multiplication for subtotal - create a for loop (with count ) to cycle through the "items", display item name, price, quantity, and subtotal. accumulate...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a pointer to an int variable. It computes the length of the string and updates the int variable with the length. stringCopy() - Copies one string onto another using pointers #include<stdio.h> #define MAX_STR_LEN 2048 void stringLength(char *str, int *len) { // This function computes the length of the string // at the address in the pointer *str. Once the length // has been determined, it...
Create C function for String.c and Strings.h that #include <string.h> /* substring - return a pointer...
Create C function for String.c and Strings.h that #include <string.h> /* substring - return a pointer to the substring beginning at the iPos-th position. * If iPos is out-of-range, return (char*)NULL */ char* substring(char* str, int iPos); /* charPosition - return the position of the first occurance of c in str. * If c not present, return -1 */ int charPosition(char* str, char c);
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Sort a string array by frequency given an array of string write a function that will...
Sort a string array by frequency given an array of string write a function that will return an array that is sorted by frequency example {"hello","hola","hello","hello","ciao","hola","hola","hola"} returned array should be {"hola","hello","ciao"}
In C Programing Create a stack using an array. Define the index variable of the array...
In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped....
C++: 1.When and in what order are constructors and destructors called? 2. Create a class without...
C++: 1.When and in what order are constructors and destructors called? 2. Create a class without any constructors, and show that you can create objects with the default constructor. Now create a non-default constructor (one with an argument, aka parameterized constructor) for the class, and try compiling again. Explain what happened.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT