Redefinition/Indirection Error Message

I'm getting this error message:

stringManipulation.cpp(178) : error C2372: 's1' : redefinition; different types of indirection


when I try to set a pointer to an array in the following code:

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
///////////////////////////////////////////////////////////////////////
//                                                                     
// Filename: stringManipulation.cpp
// 
// 
//
//             
//             
// 
// Description:
//    The string copy function written using pointers.
/////////////////////////////////////////////////////////////////////////#include <iostream>
using namespace std;

//////////////////////////////////////////////////////////////////////
//
// Function: stringCopy                                          
//                                                                   
// Description:
//    Function to copy strings using pointers.                 
//                                                                
// Parameters:  
//    firstParam  : char pointer s1 dereferenced          
//    secondParam : constant char pointer s2 dereferenced               
//                                                       
// Returns:  
//    returnVal : char s1
//                                                                     
///////////////////////////////////////////////////////////////////////	

void stringCopy( char *s1, const char *s2 )
{
     for(int i = 0; *(s1 + i) != '\0'; i++)
		*(s1 + i) = *(s2 + i);
}     

	
//////////////////////////////////////////////////////////////////////
//
// Function: stringCat                                          
//                                                                   
// Description:
//    Concatenation of strings using pointers.                 
//                                                                
// Parameters:  
//    firstParam  : char pointer s1 dereferenced          
//    secondParam : constant char pointer s2 dereferenced               
//                                                       
// Returns:  
//    returnVal : char s1
//                                                                     
///////////////////////////////////////////////////////////////////////

	char *stringCat( char *s1, const char *s2 )
{
    size_t i,j;
    for (i = 0; s1[i] != '\0'; i++)
	{
		for (j = 0; s2[j] != '\0'; j++)
		{
			s1[i+j] = s2[j];
		}
		s1[i+j] = '\0';
		return s1;
	}
}

//////////////////////////////////////////////////////////////////////
//
// Function: strcmpA                                          
//                                                                   
// Description:
//    Comparing strings using array subscripting.                
//                                                                
// Parameters:  
//    firstParam  : constant char pointer s1 dereferenced          
//    secondParam : constant char pointer s2 dereferenced               
//                                                       
// Returns:  
//    returnVal : integer value
//                                                                     
///////////////////////////////////////////////////////////////////////

int strcmpA ( const char *s1, const char * s2 )
{
	for (int i = 0; s1[i] != '\0'; i++)
	{
		if (s1[i] == s2[i])
			return 0;
		else if (s1[i] < s2[i])
			return -1;
		else return 1;
	}
}

//////////////////////////////////////////////////////////////////////
//
// Function: strcmpP                                          
//                                                                   
// Description:
//    Comparing strings using pointer arithmetic.                
//                                                                
// Parameters:  
//    firstParam  : constant char pointer s1 dereferenced          
//    secondParam : constant char pointer s2 dereferenced               
//                                                       
// Returns:  
//    returnVal : integer value
//                                                                     
///////////////////////////////////////////////////////////////////////

int strcmpP ( const char *s1, const char * s2 )
{
	for (int i = 0; *s1 + i != '\0'; i++)
	{
		if (*s1 + i == *s2 + i)
			return 0;
		else if (*s1 + i < *s2 + i)
			return -1;
		else return 1;
	}
}

//////////////////////////////////////////////////////////////////////
//
// Function: strlenA                                          
//                                                                   
// Description:
//    Comparing strings using array subscription.                
//                                                                
// Parameters:  
//    firstParam  : constant char pointer s1 dereferenced                       
//                                                       
// Returns:  
//    returnVal : aCount;
//                                                                     
///////////////////////////////////////////////////////////////////////
int aCount = 0;
size_t strlenA ( const char *s2)
{
	for (int i = 0; s2[i] != '\0'; i++)
		{
			aCount++;
			return aCount;
		}
}

//////////////////////////////////////////////////////////////////////
//
// Function: strlenP                                          
//                                                                   
// Description:
//    Comparing strings using pointer arithmetic.                
//                                                                
// Parameters:  
//    firstParam  : constant char pointer s1 dereferenced                       
//                                                       
// Returns:  
//    returnVal : aCount;
//                                                                     
///////////////////////////////////////////////////////////////////////
int pCount = 0;
size_t strlenP( const char *s2 )
{
	for (int i = 0; *s2 + i != '\0'; i++)
		{
			pCount++;
			return pCount;
		}
}

int main()
{
	char s1[ 100 ] = { 1, 2, 3 };
	char s2[ 100 ] = { 1, 2, 3 };
	char s3[ 100 ] = { 1, 2, 3 };

	char *s1 = s1;
	const char *s2 = s2;
	const char *s3 = s3;	

	stringCopy(*s1, *s2);
	stringCopy(*s1, *s3);

	*stringCat(*s1, *s2);
	*stringCat(*s2, *s3);

	strcmpA(*s2, *s3);
	strcmpA(*s3, *s2);

	strcmpP(*s2, *s3);
	strcmpP(*s3, *s2);

	strlenA(*s1);
	strlenA(*s2);

	strlenP(*s1);
	strlenP(*s2);

	system( "pause" );
	return 0;
}


I get one of these errors every time I set a pointer to point to an array in this problem. I can not figure out why.

Thanks for your help,

Adam
1
2
3
4
5
6
7
	char s1[ 100 ] = { 1, 2, 3 };
	char s2[ 100 ] = { 1, 2, 3 };
	char s3[ 100 ] = { 1, 2, 3 };

	char *s1 = s1;
	const char *s2 = s2;
	const char *s3 = s3;


You're naming these pointers the same thing as the arrays you defined immediately above them.

The compiler is confused because you have 2 variables in the same scope with the same name, and it has no way to know which variable to use.

rename your arrays.
Last edited on
Thanks Disch. That cleared up some errors and created some new ones. Now, I'm getting an error message on all of my functions that reads:

.\stringManipulation.cpp(185) : error C2664: 'stringCopy' : cannot convert parameter 1 from 'char' to 'char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast


Here's the updated code:

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
///////////////////////////////////////////////////////////////////////
//                                                                     
// Filename: stringManipulation.cpp
// 
//
//
//            
//          
// 
//
// Description:
//    The string copy function written using pointers.
/////////////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

	char s1[ 100 ] = { 1, 2, 3 };
	char s2[ 100 ] = { 1, 2, 3 };
	char s3[ 100 ] = { 1, 2, 3 };

	char *s1Ptr = s1;
	const char *s2Ptr = s2;
	const char *s3Ptr = s3;	

//////////////////////////////////////////////////////////////////////
//
// Function: stringCopy                                          
//                                                                   
// Description:
//    Function to copy strings using pointers.                 
//                                                                
// Parameters:  
//    firstParam  : char pointer s1 dereferenced          
//    secondParam : constant char pointer s2 dereferenced               
//                                                       
// Returns:  
//    returnVal : char s1
//                                                                     
///////////////////////////////////////////////////////////////////////	

void stringCopy( char *s1Ptr, const char *s2Ptr )
{
     for(int i = 0; *(s1 + i) != '\0'; i++)
		*(s1 + i) = *(s2 + i);
}     

	
//////////////////////////////////////////////////////////////////////
//
// Function: stringCat                                          
//                                                                   
// Description:
//    Concatenation of strings using pointers.                 
//                                                                
// Parameters:  
//    firstParam  : char pointer s1 dereferenced          
//    secondParam : constant char pointer s2 dereferenced               
//                                                       
// Returns:  
//    returnVal : char s1
//                                                                     
///////////////////////////////////////////////////////////////////////

	char *stringCat( char *s1Ptr, const char *s2Ptr )
{
    size_t i,j;
    for (i = 0; s1[i] != '\0'; i++)
	{
		for (j = 0; s2[j] != '\0'; j++)
		{
			s1[i+j] = s2[j];
		}
		s1[i+j] = '\0';
		return s1;
	}
}

//////////////////////////////////////////////////////////////////////
//
// Function: strcmpA                                          
//                                                                   
// Description:
//    Comparing strings using array subscripting.                
//                                                                
// Parameters:  
//    firstParam  : constant char pointer s1 dereferenced          
//    secondParam : constant char pointer s2 dereferenced               
//                                                       
// Returns:  
//    returnVal : integer value
//                                                                     
///////////////////////////////////////////////////////////////////////

int strcmpA ( const char *s1Ptr, const char *s2Ptr )
{
	for (int i = 0; s1[i] != '\0'; i++)
	{
		if (s1[i] == s2[i])
			return 0;
		else if (s1[i] < s2[i])
			return -1;
		else return 1;
	}
}

//////////////////////////////////////////////////////////////////////
//
// Function: strcmpP                                          
//                                                                   
// Description:
//    Comparing strings using pointer arithmetic.                
//                                                                
// Parameters:  
//    firstParam  : constant char pointer s1 dereferenced          
//    secondParam : constant char pointer s2 dereferenced               
//                                                       
// Returns:  
//    returnVal : integer value
//                                                                     
///////////////////////////////////////////////////////////////////////

int strcmpP ( const char *s1Ptr, const char *s2Ptr )
{
	for (int i = 0; *(s1 + i) != '\0'; i++)
	{
		if (*(s1 + i) == *(s2 + i))
			return 0;
		else if (*(s1 + i) < *(s2 + i))
			return -1;
		else return 1;
	}
}

//////////////////////////////////////////////////////////////////////
//
// Function: strlenA                                          
//                                                                   
// Description:
//    Comparing strings using array subscription.                
//                                                                
// Parameters:  
//    firstParam  : constant char pointer s1 dereferenced                       
//                                                       
// Returns:  
//    returnVal : aCount;
//                                                                     
///////////////////////////////////////////////////////////////////////
int aCount = 0;
size_t strlenA ( const char *s2Ptr)
{
	for (int i = 0; s2[i] != '\0'; i++)
		{
			aCount++;
			return aCount;
		}
}

//////////////////////////////////////////////////////////////////////
//
// Function: strlenP                                          
//                                                                   
// Description:
//    Comparing strings using pointer arithmetic.                
//                                                                
// Parameters:  
//    firstParam  : constant char pointer s1 dereferenced                       
//                                                       
// Returns:  
//    returnVal : aCount;
//                                                                     
///////////////////////////////////////////////////////////////////////
int pCount = 0;
size_t strlenP( const char *s2Ptr )
{
	for (int i = 0; *(s2 + i) != '\0'; i++)
		{
			pCount++;
			return pCount;
		}
}

int main()
{
	stringCopy(*s1Ptr, *s2Ptr);
	stringCopy(*s1Ptr, *s3Ptr);

	*stringCat(*s1Ptr, *s2Ptr);
	*stringCat(*s2Ptr, *s3Ptr);

	strcmpA(*s2Ptr, *s3Ptr);
	strcmpA(*s3Ptr, *s2Ptr);

	strcmpP(*s2Ptr, *s3Ptr);
	strcmpP(*s3Ptr, *s2Ptr);

	strlenA(*s1Ptr);
	strlenA(*s2Ptr);

	strlenP(*s1Ptr);
	strlenP(*s2Ptr);

	system( "pause" );
	return 0;
}


Thanks again for any help!

Adam
I think on lines 185 to 201 that you're supposed to not be using the deference operator (*) because the functions take pointers to chars (char*) and not chars themselves (char).
Thanks LB!

Perfect.
why did you make everything global? That destroyed all of your functions! Change it back! You shouldn't have any global variables in this program.

EDIT:

Just to reiterate: all of your functions are now broken. None of them are reading/modifying the passed strings, they're all using the globals instead. It effectively makes your functions worthless.
Last edited on
I changed code. Thanks Disch.
Topic archived. No new replies allowed.