Copying a C-string into a new block of memory

Greetings,

The exercise is about writing prototypes and definitions of the functions present in the given programme.
My questions are:
1) What's the point of the int ct in the structure? I don't use it anywhere in the programme and it works fine. Where should I have used it?
2) Have I written the functions, especially the set() function, correctly?
I'm still having trouble with pointers.

Thank you for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 // Exercise 8.4.cpp : Defines the entry point for the console application.
//
/*
Complete this skeleton by providing the described functions and prototypes. Note that there should be
two show() functions, each using default arguments, Use const arguments when appropriate. Note that set() should use new
to allocate sufficient space to hold the designated string.
*/

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <cstring>


struct stringy
{
	char * str;//points to a string
	int ct;//length of string (not counting '\0')
};


void set(stringy & b, const char * t);
void show(const stringy & b, int n = 1);
void show(const char *, int n = 1);
int main()
{
	stringy beany;
	char testing[] = "Reality isn't what it used to be.";

	set(beany, testing);//first argument is a reference,
						//allocates space to hold copy of testing,
						//sets str member of beany to point to the new block,
						//copies testing to new block,
						//and sets ct member of beany

	show(beany);
	show(beany, 2);
	
	testing[0] = 'D';
	testing[1] = 'u';
	show(testing);
	show(testing, 3);
	show("Done!");

	system("pause");
	return 0;
}

void set(stringy & b, const char * t)
{
	char * pc = new char[strlen(t) + 1];		//allocates space to hold copy of testing,
	b.str = pc;									//sets str member of beany to point to the new block,
	strcpy_s(b.str, strlen(t) + 1, t);			//copies testing to new block,
	b.ct = strlen(t);							//and sets ct member of beany
}

void show(const stringy & b, int n)
{
for (int i = 0; i < n; i++)
cout << b.str << endl;
cout << endl;
}

void show(const char * str, int n)
{
	for (int i = 0; i < n; i++)
		cout << str << endl;
	cout << endl;
}
Hello Arcy,

What's the point of the int ct in the structure?

The comment after the variable in the struct says it all.

I don't use it anywhere in the programme and it works fine.

That is because your program is not written to use what is in the struct properly. Your show functions are setup to let the cout print the string. This eliminates the need to use the "ct" variable. I am thinking that nested for loops to print each character of the string would allow you to use the "ct" variable which would make more use of the struct and the "set" function as it should be.

Note that there should be two show() functions, each using default arguments

As the instructions say "arguements" plural i.e., more than one. You have only used one default in your show functions. Works fine BTW, but not correct.

Hope that helps,

Andy
What's the point of the int ct in the structure?

In this example it seems to serve two separate but related purposes.
1. record the size of the allocated memory block.
2. record the length of the string contained in that block.

For example if somewhere later in the program you wanted to copy a new value for the contents of the string, if it will fit in the existing block, then just use strcpy(). If the new content is too large, then the program would need to delete and re-allocate the memory to the required size.

Of course that's getting ahead of where you are with this program.

But for the time being, you could at least make use of the ct variable instead of repeatedly calling the strlen() function.
1
2
3
4
5
6
7
8
// Original code:
void set(stringy & b, const char * t)
{
    char * pc = new char[strlen(t) + 1];        //allocates space to hold copy of testing,
    b.str = pc;                                 //sets str member of beany to point to the new block,
    strcpy_s(b.str, strlen(t) + 1, t);          //copies testing to new block,
    b.ct = strlen(t);                           //and sets ct member of beany
}



1
2
3
4
5
6
7
// Suggested version, making use of ct
void set(stringy & b, const char * t)
{
    b.ct  = strlen(t);                 // set size of string 
    b.str = new char[b.ct + 1];        // allocates sufficient space to hold string 
    strcpy_s(b.str, b.ct + 1, t);      // copy string contents into allocated block 
}

Thank you to both of you for your help.
Topic archived. No new replies allowed.