Reputation: 1
char *str[]={"we", "will", "teach", "you"};
int count=0;i,len,m;
The sizeof(str) should return the value 18, while it returns a value 32 in codeblocks. Moving forward if it returns the length of the string correctly, knowing the fact that this character array is stored in continous memory location, we can count the number of times "\0" appears as we traverse our array.
And this value would give us the index of the last string. I tried in the following manner:
int len=sizeof(str);
m=str[0]; //assign the base address of 1st string
for(i=0;i<len;i++)
{
if(*(m+i)=="\0") //compare the value at memory location with null
count++;
}
printf("%d", count);
If somebody could provide me with an insight whether my approach is correct, or some other solution. Basically want to find the index of the last string, or the number of strings present, when the number of strings in the array cannot be counted.
Upvotes: 0
Views: 988
Reputation: 43
Damn, it's been a long time doing programming in C/C++.
---Note the below code has 'Warnings' especially deadly 2 ones by todays Standards--
I couldn't fix them because I am a bit rusty in those concepts.I will have to go through them. Anyways you should be able to fix them if you have been playing around with C/C++ for some time.
#include<stdio.h>
#include<string.h>
int main()
{
char *str[]={"we", "will", "teach", "you"};
char **ptr;
int i,baseAddr,index;
ptr=&str;
baseAddr=(*ptr+0);
for(i=0;i<4;i++)
{
//printf("%u\n",str[i]);
baseAddr=baseAddr+strlen(str[i])+1;
if(i==3)
{
index=i;
}
}
printf("Last Element Address:%d",baseAddr-strlen(str[index])-1);
return 0;
}
//C++?
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char *str[]={"we", "will", "teach", "you"};
char **ptr;
int i,baseAddr,index;
ptr=str;
baseAddr=(int)(*ptr+0);
for(i=0;i<4;i++)
{
cout<<&str[i]<<endl;
baseAddr=baseAddr+strlen(str[i])+1;
if(i==3)
{
index=i;
}
}
cout<<"Last Element Address:"<<(ptr+index);
return 0;
}
Upvotes: -1
Reputation: 781
I don't see anything that directly answers the question of "How can we return the index of the last string" so...
Given your example, you simply need to use:
int strCount = sizeof(str) / sizeof(char*);
char* lastString = str[strCount-1];
You can't walk each string to the end because they may not be contiguous. You have to directly find the last index of the array using sizeof. The contents or location of the previous strings won't help.
The reason sizeof(str) is returning 32, is because you are probably compiling a 64-bit application which makes each char* take up 8 bytes. Four strings * 8 bytes = 32 bytes.
Upvotes: 1
Reputation: 27538
char *str[]={"we", "will", "teach", "you"}; int count=0;i,len,m;
The
sizeof(str)
return the value 18, while it returns a value 32 in codeblocks.
18 is impossible if your compiler behaves correctly. It's because the result of sizeof
here has nothing to do with the total length of all strings. It returns the size of a char*
, which is implementation-dependent, multiplied by the number of elements in the array. And 18 cannot be divided by 4.
Try something less confusing, something like short arr[] = { 1, 2, 3, 4, 5 };
. It returns 10 on my machine, because sizeof(short)
is 2 on my machine and the array has 5 elements, and 2 * 5 = 10
.
knowing the fact that this character array is stored in continous memory location
It is not guaranteed to be stored like this. Not at all, in fact. The individual string literals are in contiguous memory, and the pointers to those literals are contiguous, too. That's all. In other words, you could very well have a situation like this:
+-----------------+
| |
v |
"we\0" "teach\0" "will\0" | "you\0"
^ ^ | ^
| | | |
| +---------+ | |
| | | |
| | +---------+ |
| | | |
| | | +-----+
| | | |
Pointer1|Pointer2|Pointer3|Pointer4
If somebody could provide me with an insight whether my approach is correct
Your approach invokes undefined behaviour. You try to access memory after the terminating \0
of str[0]
, because len
is greater than the length of str[0]
. You also compare with "\0"
instead of '\0'
, which does not make sense, because the string literal "\0"
is yet at another unspecified location in memory.
There are some other details wrong with your approach, but since it's based on a false premise, there's not much you can do to save it.
or some other solution.
If the problem is really "How can we return the index of the last string or the base address of last string?", then just remember the size of the array separately and do size - 1
. If you are actually using C++, just use std::array
or std::vector
.
Upvotes: 4
Reputation: 10064
knowing the fact that this character array is stored in continous memory location
You can't really do this. Consider this code which displays the memory layout of strings.
#include <stdio.h>
char * spoiler = "you";
char * str[] = {"we", "will", "teach", "you"};
int main (void)
{
int i;
for (i = 0; i < 4; ++i)
{
printf ("%p, %i, %s\n", str[i], str[i] - str[0], str[i]);
}
return 0;
}
Output:
10b80, 0, we
10b88, 8, will
10b90, 16, teach
10b78, -8, you
The strings are not contiguous, they're being put on 8-byte memory boundaries. And they're not even in the right order.
The computer makes no promises about the relative locations of strings in memory and it's even possible for different strings to use overlapping memory.
Upvotes: 1
Reputation: 1495
char *str[]={"we", "will", "teach", "you"};
this implies str is an array of pointers of type char.
so, str[0] points to we, str[1] points to will and so on.
str does not point to one contiguous string.
so, str[3] will point to last string "you".
Upvotes: 2