edited by
2,520 views
2 votes
2 votes
char * a[] = "red shoes";

How the space in string constant "red shoes" will be stored in 1-D array in memory?

edited by

4 Answers

4 votes
4 votes

Addresses will be stored in consecutive locations if we assume that base address is 1000

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
edited by
3 votes
3 votes

i think you're syntax is wrong...

if you want want to store that string it should be like this

char a[] = "red shoes";

and if you're asking about what is stored at blank space(at an index of blank space inside the array), well its just a literal in c language just like others & having an ascii value

-- space (ASCII 0x32, C literal ' ')    

Hope it helps.. :)

2 votes
2 votes
char * a[] = {"red shoes"};      // array of 1 character pointer pointing to base address of string constant.

(  only 1 but it can be extended like {"red shoes", "blue tie"}; now we have array of 2 pointers. 1st pointer is pointing to continuous locations containing 9 character and 2nd pointer is pointing to another continuous locations containing 8 characters.

Content will be addresses. Here, only 1 address in case of "red shoes". 

a[0] = base address of 1st pointer. ie. 1000 (this pointer points to our characters ie. within 9 blocks)

a[1] = base address of 2nd pointer. ie 1010 because of gap of 9 + 1 null character.

a[2] = base address of 3rd pointer. lets say 1025 and so on..... thus this pointers locations are non-continuous locations. See 1000, 1010, 1025.....

our 1st pointer will point to all the characters at continuous memory locations.

ie. a[0] = contains base address of  1st string constant = 1000

a[0][0] =  r and  &(a[0][0]) =1000

a[0][1] =  and  &(a[0][1]) =1001 (yes address of a[1] != 1000)

a[0][2] =  and  &(a[0][2]) =1002

a[0][3] =  ' ' and  &(a[0][3]) =1003

a[0][4] =  and  &(a[0][4]) =1004

a[0][5] =  and  &(a[0][5]) =1005

a[0][6] =  and  &(a[0][6]) =1006

a[0][7] =  and  &(a[0][7]) =1007

a[0][8] =  and  &(a[0][8]) =1008

a[0][8] =  ' \0 ' and  &(a[0][9]) =1009  its the ending of string constant so NULL

I used this program to understand it..

copy paste it in devcpp and run it for yourself to understand better.

#include<iostream>
using namespace std;

int main()
{
    char* a[] = {"red shoes","blue tie"};
    printf("%u %u %u %u %u %c %c ", a[0], a[1], &(a[0]), &(a[0][0]), &(a[0][1]), a[0][0], a[0][8]);
    //printf("%u %c %u %u %c %c %c %c",a[0], *(a[0]+1), &(a[0][0]), &(a[0][1]), a[0][0], a[0][1], a[1][0], a[1][1]);
}

edited by
0 votes
0 votes
There is an error in your declaration.

either use

char a[ ]="abc" ;

or use

char *a="abc";

both works same.

Related questions

0 votes
0 votes
0 answers
1
Na462 asked Nov 19, 2018
531 views
3 votes
3 votes
2 answers
3
sabir asked Nov 24, 2015
904 views
Main () { int a [3] [4] = $\begin{pmatrix} 1&2&3&4 \\ 5&6&7&8 \\ 9&10&11&12 \\ \end{pmatrix}$ printf ("\n% u% u% u", a[0]+1, * (a[0] + 1), *(*(a + 0)+1)); }What is the o...
5 votes
5 votes
6 answers
4
Pankaj Joshi asked Jan 26, 2017
15,862 views
Consider 3 dimensional Array A[90] [30] [40] stored in linear array in column major order. If the base address starts at 10. The location of A[20] [20] [30] is __________...