0

I'm attempting to learn C and going through the K&R book. Many examples online seem to use pointers to return a value from a function. I would think then that the same would be used for this K & R function:

/*
 Reverse a string in place
 */
void reverse(char s[])
{
    int c, i, j;
    for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
    {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

int main()
{
    char s[] = "HELLO";
    reverse(s);
    printf("%s", s);
    return (0);
}

I would think that the string would NOT be reversed in this situation. Yet it prints the char array backwards as originally intended by the author.

How does it do that? I don't completely understand pointers yet but I was thinking it would be like reverse(&s) and then void reverse(char *s[]) {...}

  • take a pen and a paper. Executere the program using those tools. Everything will become clear – 0___________ Jan 29 '19 at 18:58
  • 3
    Possible duplicate of [In C, are arrays pointers or used as pointers?](https://stackoverflow.com/questions/4607128/in-c-are-arrays-pointers-or-used-as-pointers) Accepted answer should help – Phil M Jan 29 '19 at 18:59
  • 5
    `void reverse(char s[]);` is 100% absolutely the same as `void reverse(char *s);` – pmg Jan 29 '19 at 18:59
  • The parameter, `s`, is a `char *`, for all that the notation `char s[]` was used in the parameter list. It's a straightforward use of array subscripting. As the argument is not a string literal, it should work fine (as long as you don't pass an empty string to the function). – Jonathan Leffler Jan 29 '19 at 18:59
  • imagine `int a[] = {1,2}; int c; c=a[0]; a[0] = a[1]; a[1] =c;` What happens? – 0___________ Jan 29 '19 at 19:00
  • @P__J__ OP isn't asking HOW the reverse works, they're asking WHY it works (i.e., why an array works like a pointer) – Phil M Jan 29 '19 at 19:04
  • @PhilM It is exactly the same. – 0___________ Jan 29 '19 at 19:09
  • Thanks for all the help... coming from a higher level language... I understand the algorithm, but not so much how the char array is returned back to the main function? –  Jan 29 '19 at 19:18

4 Answers4

2

Since no copy of the string is made anywhere the original string must be modified by the array assignments in reverse. Changing void reverse(char s[]) to void reverse(char *s) would change nothing. An array of unknown size behaves just like a pointer to the array's first element.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
2

Arrays, when passed as arguments to functions, decay to a pointer to their first element. Hence when you pass an any type of array (including a string) into a function, you are effectively passing it by reference and any modifications made to the array within the function will be reflected in the calling code as well, after the function call.

Examine the output of this code to enrich your understanding of pointers and arrays:

#include <stdio.h>
#include <stdlib.h>

void foo(int *arg, size_t len)
{
    size_t i;
    printf("sizeof arg is %zu\n", sizeof arg);
    for(i = 0; i < len; i++)
    {
        printf("arg[%zu] = %d\n", i, arg[i]);
    }
    printf("arg's address is %p\n", (void *) arg);
}

int main()
{
    int array[10] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
    printf("sizeof array is %zu\n", sizeof array);
    printf("array begins at %p in memory\n", (void *) array);
    foo(array, 10);
    return 0;
}
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • 2
    I like both answers but I picked yours because of the example. I guess that's why the book calls the function "reverse in place" –  Jan 29 '19 at 19:26
1

I also learned C from K&R, it's one of the best books out there, although after reading it, get a book that covers C99 and C11.

If you look at section 5.3 in the book

5.3 Pointers and Arrays

They write:

When an array name is passed to a function, what is passed is the location of the initial element. Within the called function, this argument is a local variable, and so an array name parameter is a pointer, that is, a variable containing an address.

So although arrays and pointers are different, when you pass an array to a function it does not pass the array, but a pointer to the first element.

some differences:

There is one difference between an array name and a pointer that must be kept in mind. A pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal.

One thing to keep in mind about K&R book when you read it. They mention something once, and 50 pages later they use it, unless you remember it, it will look like it came out of nowhere. They don't repeat themselves much in the book.

Manny_Mar
  • 398
  • 2
  • 13
0

The array will implicitly be converted to a pointer to the first element of the array when passed as an argument to a function.

That is why the the elements of the original array are modified and not a copy of it since no copy of the array is made; only the address of the first element.

machine_1
  • 4,266
  • 2
  • 21
  • 42