### Thread: Converting arrays to pointers

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep Power
0

#### Converting arrays to pointers

I have a homework problem where I need to convert a function written with arrays to one with character pointers. The code is below. Im confused on how to even begin. Any pointers? get it... pointers?? ...lol... :rolleyes:

Code:
```// Function to compare two character strings
int compareStrings (const char s1[], const char s2[])
{
while ( s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0' )
++i;
if ( s1[i] < s2[i] )
answer = -1; /* s1 < s2 */
else if ( s1[i] == s2[i] )
answer = 0; /* s1 == s2 */
else
answer = 1; /* s1 > s2 */
}```
2. I've got pointers.
Read about pointers and arrays from a few different sources. Here's a source that I didn't read.
3. With a few important exceptions, array names are equivalent to pointers. So you can refer to an array by name as a pointer, which means that the first step would be to replace the array notation in the parameter list with pointer notation.

Now, when you refer to an array with a pointer, you can still use array subscripting with that pointer, so just changing the parameters from array notation to pointer notation would work. However, I assume that you're also required to replace the subscripting in the function with pointers. I customarily declare two local char pointers, initialize them to the two pointer parameters, and then dereference them for comparisons and increment them to work my way through the strings.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2012
Posts
33
Rep Power
8
fundamentally arrays and pointers are very alike except arrays are like constant pointers i.e. non-dynamic. pointers are free to reference memory anywhere. consider this:

Code:
```int main()
{
int a[5];
int *p;

//ways you can create values for this array

p = a; // same thing as p = &a;

*p = 10; //the value pointed to by p (anArray[0]) = 10
p++;
*p = 20;  // a[1] = 20
p = &a[2];
*p = 30; // a[2] = 30
p = number + 3;
*p = 40; // a[3] = 40
p = numbers;
*(p+4) = 50; // a[4] = 50
}```
If you were to loop through this array it would obvious give you

10, 20, 30, 40, 50,

hope this helps

• C505 agrees : Thank you for your help
5. No Profile Picture
bdb
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
156
Rep Power
38
object[42] is exactly the same things as *(object + 42) whether object is an array or a pointer. So where you have an array indexing, replace with a pointer dereference.

Code:
```/* ********
while ( s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0' )
++i;
******** */
while ( *(s1 + i) == *(s2 + i) && *(s1 + i) != '\0'&& *(s2 + i) != '\0' )
++i;```
But don't ever think arrays and pointers are the same thing!
I suggest you read section 6 of the comp.lang.c FAQ (in fact, all sections).

• C505 agrees : Thank you for your help
Last edited by bdb; November 10th, 2012 at 02:31 PM.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep Power
0
Thanks to all for the replies and the links. I should have RTFM a bit more before posting, I appreciate the help.
Last edited by C505; November 11th, 2012 at 11:01 AM. Reason: typo
7. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep Power
0
I didn't write the comparestrings function, it is from a textbook. I haven't come across a function that wants a const char as an argument, I do not understand how to call the function. I have tried calling it with arrays, calling it with integers, and chars, but I always get a compile error. Before I attempt to convert it to a function of pointers I would like to have statements to print values and addresses to keep track of them, but I can not even get the output to print. I need a little nudge, any help would be most appreciated.

Code:
```#include <stdio.h>

int compareStrings (const char s1[], const char s2[])
{
while ( s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0' )
++i;
if ( s1[i] < s2[i] )
answer = -1; /* s1 < s2 */
else if ( s1[i] == s2[i] )
answer = 0; /* s1 == s2 */
else
answer = 1; /* s1 > s2 */
}

int main()
{
int result;
result = compareStrings(1,2);    // How to format argument?

//Compile error here
//[Warning] passing arg 1 of `compareStrings' makes pointer
// from integer without a cast  Whats a cast?

}```
8. You lack fundamental concepts, so many it's hard to choose one. I choose
ASCIIZ
as being immediate though far from most critical.

compareStrings("this","that");
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep Power
0
Originally Posted by b49P23TIvg
You lack fundamental concepts, so many it's hard to choose one.
lol.... clearly. However you could have sent me a link for the history of the breakfast burrito and I would have gotten more out of it. I read the wiki article, I dont see how it applies... maybe choose another fundamental concept to verse me on?
10. You called the function intended for string arguments with integers.

compareStrings("this","that");

The argument "this" passes a pointer as a function argument.

1 gets converted to a pointer and the pointer 1 is passed to the function.

When compareStrings looks for the first character of s1 it dereferences either the pointer to the ASCIIz string "this", which makes sense, or, with your use it dereferences (looks in the memory location 1) which the function would treat as an ASCIIz string if it had the chance. I'm pretty sure my operating system (and yours as well) will raise a memory fault upon dereferencing memory location 1 without superman privilege.
11. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep Power
0
I understand what the function wants, I really do. I just dont know the syntax. I will look it up. Thanks for the help.

As far as superman goes, if I was superman id de-reference the whole damm thing with some laser vision or something. I mean the guy got shot in the eye, and the bullet bounced off, and I doubt he knows the first thing about compilers.
12. Sorry, all my fault. I actually thought that by
result = compareStrings(1,2);
you were trying to use your function to compare integers 1 and 2. Ha hahah!

These fancy new intel chips actually have some sort of machine instruction to enable "super duper privilege" allowing access any part of memory, as I dredge up a memory from the last millennium.
13. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep Power
0
Would you mind checking this code and telling me what you think? It will compile but crashes after the first scanf. (The first block is commented out, to refer to the original program)

Code:
```#include <stdio.h>
#include <stdbool.h>
#include <conio.h>

// convert program to pointers

/* int compareStrings (const char s1[], const char s2[])
{
while ( s1[i] == s2[i] && s1[i] != '\0'&& s2[i] != '\0' )
++i;
if ( s1[i] < s2[i] )
else if ( s1[i] == s2[i] )
else
}
*/

int compareStrings (char *s1, char *s2)
{
int i = 0;

while ( *(s1+i) == *(s2+i) && *(s1+i) != '\0'&& *(s2+i) != '\0' )
++i;
if ( *(s1+i) < *(s2+i) )
else if ( *(s1+i) == *(s2+i) )
else
}

int main()
{
int result;
char s1[5];
char s2[5];
printf("\n compare strings");
printf("\n enter string 1: ");
scanf("%s,*s1");
printf("\n enter string 2: ");
scanf("%s,*s2");
result = compareStrings(s1,s2);
getch();
}```
14. scanf("%4s",s1);

The 4 prevents buffer overrun.
The " moved to end the format string.
*s1 would be an unassigned character, and I can't think of a sufficiently strong slang nasty to write about that. Reread the dwise, bdb, and puritylake posts you praised.
15. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
23
Rep Power
0
Originally Posted by b49P23TIvg
scanf("%4s",s1);

The 4 prevents buffer overrun.
The " moved to end the format string.