Array and Char with function

Pages: 12
@Peter87 You should change the String constructor to accept a const char*. I mentioned this in an earlier post.

How can I do , can you write for me?

Thanks a lot.
Last edited on
Change the constructor String(char * other_str); to String(const char * other_str);

as Peter87 already suggested.
@coder777

I changed the constructor in header and cpp but now problem on:
m_str = (other_str ? my_strdup(other_str) : other_str);

Error:

1
2
3
4
Error (active)		a value of type "const char *" cannot be assigned to an entity of type "char *"	


Error	C2440	'=': cannot convert from 'const char *' to 'char *'	


What is your advice?

Thanks a lot.
Peter87 already provided the code in an earlier post
http://www.cplusplus.com/forum/general/205885/#msg974544


1
2
3
4
String::String(const char * other_str)
{
	m_str = (other_str ? my_strdup(other_str) : nullptr);
}


Note the use of nullptr.
Note the use of nullptr.


It is working thank you.
Question is updated*

Now i have to add this codes to main, What is your advice for that?
It is ready in main now.

1
2
3
4
5
6
7
8
9
  
char const* hello_world_str = "Hello World";
    String string1 = hello_world_str;
    TEST_TRUE(string1.length() == 11);
    TEST_TRUE(string1.equals("Hello World"));

    char const* char_ptr = string1.charArray();
    TEST_TRUE(char_ptr != hello_world_str);
  


Should I create new constructor or new function for charArray function?

Thanks a lot.
Last edited on
Should I create new constructor or new function for charArray function?
Do you know what a function or a constructor is?
yes, i am trying.

Please help to me about:
1
2
3
4
5
6
7
char const* hello_world_str = "Hello World";
    String string1 = hello_world_str;
    TEST_TRUE(string1.length() == 11);
    TEST_TRUE(string1.equals("Hello World"));

   char const* char_ptr = string1.charArray();
    TEST_TRUE(char_ptr != hello_world_str);


thanks a lot.
Last edited on
Presumably you would want the pointer char_ptr to hold the address of its own fresh copy of the string data? Or do you want it to hold the address of the internal string data of the String object? Depending on what you aim to achieve, you would write different code.
Last edited on
@Chervil

I am using for this:

1
2
3
4
5
6
7
char const* hello_world_str = "Hello World";
    String string1 = hello_world_str;
    TEST_TRUE(string1.length() == 11);
    TEST_TRUE(string1.equals("Hello World"));

    char const* char_ptr = string1.charArray();
    TEST_TRUE(char_ptr != hello_world_str);


Then i will create:

1
2
3
string1.setCharAt(0, 'h');
    TEST_TRUE(string1.equals("hello World"));
    TEST_TRUE(string1.charArray() != hello_world_str);


You can see my codes in first message of the topic.
What is your advice? I am not sure. Because i can't change, This file should not be modified. I will make something in header file or other cpp.

Thanks a lot.
Last edited on
Is there an original question which you were given, or a program specification from which you are working?

What is this line supposed to be achieving according to those requirements?
 
    char const* char_ptr = string1.charArray();



What is your advice?

My advice is to write code which follows whatever was asked in the original problem or question or task which you have been set.

If you aren't sure, then post the original question here.
Last edited on
@Chervil

What is this line supposed to be achieving according to those requirements?


It is for just check. For example Line 3 and Line 6 .

1
2
3

    TEST_TRUE(string1.equals("hello World"));
    TEST_TRUE(string1.charArray() != hello_world_str);


Thanks.
Last edited on
No, that isn't what I asked about.
I was asking what is this line supposed to be achieving:
 
    char const* char_ptr = string1.charArray();


@Chervil

This program is C++ so that maybe like the STL function c_str . I can create my_c_str function for charArray(). But how?

Thanks.
Last edited on
Let charArray() return m_str. That will fulfill the unit test.
@coder777
Let charArray() return m_str. That will fulfill the unit test.



Just like this?

1
2
3
4
char const* String::charArray() 
{
	return m_str;
}



when i wrote this for this :

1
2
3
4
5
6
7
8
9
10
  
#define TEST_TRUE(x) printf("Test '%s' %s\n", #x, (x) ? "passed" : "failed!")

  char const* hello_world_str = "Hello World";
    String string1 = hello_world_str;
    TEST_TRUE(string1.length() == 11);
    TEST_TRUE(string1.equals("Hello World"));

    char const* char_ptr = string1.charArray();
    TEST_TRUE(char_ptr != hello_world_str);


built is succes. And result:

1
2
3
Test 'string1.length() == 11' passed
Test 'string1.equals("Hello World")' passed
Test 'char_ptr != hello_world_str' passed



Thank you....

------------------------

Updated* ( Please check first post)

Can I ask, I have to create and write something for setCharAt( ) function.

1
2
3
4
  string1.setCharAt(0, 'h');
    TEST_TRUE(string1.equals("hello World"));
    TEST_TRUE(string1.charArray() != hello_world_str);
    TEST_TRUE(strcmp(hello_world_str, "Hello World") == 0);


Whar is your advice for setCharAt( ) ?
Last edited on
Updated* ( Please check first post)

Can I ask, I have to create and write something for setCharAt( ) function.

1
2
3
4
5
6
7
8
9
10
11
12
char const* hello_world_str = "Hello World";
    String string1 = hello_world_str;
    TEST_TRUE(string1.length() == 11);
    TEST_TRUE(string1.equals("Hello World"));

    char const* char_ptr = string1.charArray();
    TEST_TRUE(char_ptr != hello_world_str);

  string1.setCharAt(0, 'h');
    TEST_TRUE(string1.equals("hello World"));
    TEST_TRUE(string1.charArray() != hello_world_str);
    TEST_TRUE(strcmp(hello_world_str, "Hello World") == 0);



What is your advice for setCharAt( ) ?
Last edited on
Topic archived. No new replies allowed.
Pages: 12