Pointer to some characters in a string

#include<iostream>
using namespace std;
int main()
{
    char s1[80]={"This is a developed country."};
    char *s2[8];
    s2[0]=&s1[10];
    cout<<*s2;    //Predicted OUTPUT: developed
                    // Actual OUTPUT: developed country.
    return 0;
}

      

I want cout <* s2; should only print the letters {"developed"} in it, so I gave the length * s2 [8] as 8 characters. What can I do so that the cout <<* s2 variable only displays length up to 8 characters. I am using dmc, lcc and OpenWatcom compilers. This is just a small part of another larger program in which I am using a string data type, so what can I do now, and thank you very much for answering my question :)

+3


source to share


6 answers


s2

- an array with a length of 8 pointers to char

. You make the first point of the element s1

starting at position 10. That's all. You don't use the rest of this array. Therefore, the length s2

does not matter.

You could do this instead:

char* s2 = &s1[10];

      

If you want to create a string from a part s1

, you can use std::string

:

std::string s3(s1+10, s1+19);
std::cout << s3 << endl;

      



Note that this allocates its own memory buffer and contains a copy or original character sequence. If you only need to look at a portion of another string, you can easily implement a class that contains the beginning and one end of the end pointer to the original. Here's a rough sketch:

struct string_view
{
    typedef const char* const_iterator;
    template <typename Iter>
    string_view(Iter begin, Iter end) : begin(begin), end(end) {}

    const_iterator begin;
    const_iterator end;
};

std::ostream& operator<<(std::ostream& o, const string_view& s)
{
    for (string_view::const_iterator i = s.begin; i != s.end; ++i)
      o << *i;
    return o;
}

      

then

int main()
{
    char s1[] = "This is a developed country.";
    string_view s2(s1+10, s1+19);
    cout << s2 << endl;
}

      

+5


source


s2

- an array of pointers to char*

. You always use the zero element in this array.

&s1[10]

points to the 11th character on the line s1

. This address is assigned to element zero s2

.



The instructions are cout

*s2

equivalent s2[0];

. So, it cout << *s2;

outputs the zero element s2

that was assigned to the 11th character s1

. cout

will translate along memory until the null terminator of your string is reached.

+1


source


Lines must be NULL terminated aka \0

. The start is s2

great, but cout will continue reading until the end. You have to actually copy the data, not just provide a pointer if you want it to be outputted.

+1


source


Your mistake is that

char *s2[8];

      

declares a pointer to an 8-character array (or equivalently, a pointer to a string with exactly -8 characters). He does neither one nor the other. Instead of declaring a pointer to an array, it declares an array of pointers.

If you want to s2

be a pointer to an 8 character array, you need:

char (*s2)[8];

      

But it still messed up. You're asking:

What can I do to make the variable * s2 only store up to its length?

Do you think its length is 8? Before trying to answer this, go back to the definition s1

:

char s1[80]={"This is a developed country."};

      

Is the length 80 or 28? The answer will be either depending on how you define "length" - the length of the array or the length before the null terminator?

All these misconceptions about size are useless. As @nm pointed out in a comment, the solution to all problems with pointers in C ++ is to stop using pointers. (Sorry if I paraphrased nm incorrectly!)

#include<iostream>
using namespace std;
int main()
{
    string s1="This is a developed country.";
    string s2;
    s2 = s1.substr(10, 9);
    cout << s2;
    return 0;
}

      

+1


source


If you want to do it in a ghetto style and skip std :: string, for some reason you can always use strncpy, memcpy, or strstr, etc.

int main()
{
    char s1[80]="This is a developed country.";
    char s2[10];
    strncpy(s2,s1+10,9);
    s2[9] = '\0';

    std::cout << s2 << std::endl;

    std::cin.get();
    return 0;
}

      

+1


source


s2 is of type char arry, element arry is char *, so you cannot use it to store a string. if you want to get "designed" in lines, you can write code like this:

#include<iostream>
using namespace std;
int main()
{
    char *s1[]={"This", "is", "a", "developed", "country."};
    char *s2[8];
    s2[0]= s1 + 3;
    cout<<s2[0];    //Predicted OUTPUT: developed
                    // Actual OUTPUT: developed country.
    return 0;
}

      

+1


source







All Articles