converting arrays to pointers

hello everyone...

i have this class assignment and i need to convert my functions that use arrays to functions that use pointers that do the same thing...

here is one part of the code that needs to be converted...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

void strcat(char t[], const char s[])
{
	unsigned int i;

	for (i = 0; t[i]; i++);
	strcpy(t+i, s);
}

/*
void strcatPT(char *t, const char *s)
{
	unsigned int i;
	
	for(i = 0; i 
*/



you can see where i commented out the code that i was trying but got stuck on the for loop...




this is the 2nd part that i need help with...


Give the pointer version of the function strcmp(). This function returns 0, if the two char-arrays are equal (the same); a negative number, if target is < than source; and a positive number, if the target is > source. Here are some example:

strcmp("abc", "abc") returns 0
strcmp("abc", "abe") returns -2
strcmp("abcdefg", "abe") returns -2
strcmp("ab", "aaa") returns 1
strcmp("abcd", "abc") returns 100
strcmp("abc", "abcd") returns -100


now i don't want anyone to do the entire code for me otherwise i will never learn but maybe someone can help start me in the right direction. i thank you all for your time and consideration

thank you all again!
I'm guessing the point of the exercise is to make sure you understand pointer notation. Remember that because array parameters immediately undergo the array-to-pointer conversion you're already dealing with pointers here.

If you need to re-write the syntax, you can do so by remembering that array subscripting a[i] is semantically equivalent to *(a + i).

Here is one "array" version of strcmp
1
2
3
4
5
6
7
int strcmp(char const* const a, char const* const b) {
  int i = 0;  
  while (a[i] && a[i] == b[i]) // find first differing char
    ++i;
  
  return (a[i] < b[i])? -1: a[i] > b[i];
}

http://coliru.stacked-crooked.com/a/2f36157193eca8b3

Hint: if you modify a and b, you can eliminate the index i.
Last edited on
mbozzi

thank you so much for your response...

could you please elaborate on your hint and could you also breakdown your return line for me

1
2
3

return (a[i] < b[i])? -1: a[i] > b[i];


also do you think you could assist me with this code and rewriting it with pointers

1
2
3
4
5
6
7
8
9

void strcat(char t[], const char s[])
{
	unsigned int i;

	for (i = 0; t[i]; i++);
	strcpy(t+i, s);
}



i really want to learn here so if you can help break it down a bit for me i would owe you big time!



thank you so much i really appreciate the assistance!
Last edited on
In this context, the conditional operator is equivalent to
1
2
3
4
if (a[i] < b[i]) {
  return -1; 
else 
  return a[i] > b[i]; // note:  function returns int 

Here, a[i] is the first character which differs from b[i] between the two strings, or the null terminator of a[i]. This character is the one which resolves the lexicographcal comparison of a and b when compared against b[i].
If and only if a[i] is ordered before b[i], the result is -1 (less than zero).
If and only if a[i] is ordered after b[i], the result is 1 (the value of true as an integer).
Otherwise, the strings are lexicographically equal, so the result is 0 (false as an integer).

As I wrote, a[i] is exactly the same as *(a + i).
Also since array parameters decay to pointers, void strcat(char t[], const char s[]) is exactly equivalent to
void strcat(char* t, const char* s):
1
2
3
4
5
void strcat(char* t, const char* const s) {
	unsigned int i;
	for (i = 0; *(t + i); i++);
	strcpy(t+i, s);
}

The last and least mechanical change you can make is to increment the pointer itself instead of using an extra index. This is what I meant by my hint:
1
2
3
4
void strcat(char* t, const char* const s) {
  while (*t) ++t; // find the null-terminator of the destination string.
  strcpy(t, s);
}

The same process can be applied to strcmp or probably to most functions operating upon C-strings.
Last edited on
thank you so much for your response!

i think i am almost there but i keep getting this one error and i am really hoping maybe you can assist me with it i know that you have done so much already so i really can't thank you enough for your time!

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

#include <iostream>

using namespace std;


unsigned int strlen(const char s[])
{
    unsigned int n;
    for (n = 0; s[n]; n++);
    return n;
}


unsigned int strlenPT(const char * s)
{

    unsigned int n;
    for (n = 0; *(s+n); n++);

    return n;
}

//copying array 
void strcpy(char t[], const char s[])
{
    for (int i = 0; t[i] = s[i]; i++);
}


void strcpyPT(char * t, const char * s)
{
    for ( ; *t++ = *s++; );
}

void strncpy(char t[], const char s[], const unsigned int n)
{
    unsigned int i;
   
    for (i = 0; i < n and s[i]; i++)
        t[i] = s[i];
    t[i] = '\0';
} 

void strncpyPT(char * t, const char * s, const unsigned int n)
{
    unsigned int i;

    for (i = 0; i < n and *(i + s) != '\0'; i++) {
        *t++ = *s++;
    } 

    *t = '\0'; //null character 
}

void strcat(char t[], const char s[])
{
	unsigned int i;

	for (i = 0; t[i]; i++);
	strcpy(t+i, s);
}


void strcatPT(char *t, const char *s) {
	unsigned int i;
	
	for (i = 0; *(t+i); i++);
	strcpy(t+i, s);
}

main() 
{

    char s[] = "Hello";
    char t[] = "World";

    

    cout << "Length of Array: " << strlen(s) << endl;
    cout << "Length of Array: " << strlenPT(t) << endl;
   
    strcpy(s,t);
    cout << endl;
    cout << "Copy of Array: " << t << endl;
    
    strcpyPT(s,t);
    cout << "Copy of Array: " << t << endl;
    
    strncpy(s,t,3);
    cout << endl;
    cout << "String Copy of Array: " << s << endl;

    strncpyPT(s,t,3);
    cout << "String Copy of Array: " << s << endl;
	
	strcat(s,t);
	cout << "String 1 and 2 together are: " << strcat(s[i],t[i]) << endl;
	
	strcatPT(s,t);
	cout << "Pointer version of string 1 and 2 together are: " << strcatPT(*(s+i),*(t+i)) << endl;


}


the error i get is...

main.cpp: In function 'int main()':
main.cpp:97:54: error: 'i' was not declared in this scope
cout << "String 1 and 2 together are: " << strcat(s[i],t[i]) << endl;
^

any ideas why??

strcat concatenates two strings. s[i] (even if i existed) would just be a single character.

That means the call must look like strcat(s, t) in both cases.
Note that even if this is changed, strcat assumes that the destination buffer is large enough to hold both the source and destination strings, and this is not the case. Make the destination buffer larger.

http://en.cppreference.com/w/cpp/string/byte/strcat
Last edited on
mbozzi you are the man!

i can't thank you enough i was finally able to get this thing to work correctly as it should...

you really saved me with this line...

1
2
3
4
5
6
7
8
9

int strcmp(char const* const a, char const* const b) {
  int i = 0;  
  while (a[i] && a[i] == b[i]) // find first differing char
    ++i;
  
  return (a[i] < b[i])? -1: a[i] > b[i];
}



but what i ended up doing was returning the difference of the ASCII code

so i changed it a bit to...

return(*(a+i) - *(b+i)) which gave me exactly what i was looking for...

can't thank you enough again!

you are really talented!

thanks again

Topic archived. No new replies allowed.