user5588864
user5588864

Reputation:

no ampersand sign for character array pointer in scanf function in C

I'm doing an exercise using a lookup function with a character array word in a dictionary program. My question concerning this exercise is the use of the pointer in the scanf function in main. The normal functionality of a scanf function is to add an ampersand before the pointer to recognize the input as a pointer. However, in this code example I found that the use of the ampersand sign was non-existent. Can anyone explain to me the reason why scanf allows word instead of &word? Thanks.

// Dictionary lookup program using Binary Search

#include <stdio.h>

struct entry 
{
    char word[15];
    char definition[50];
};

// Function to compare two character strings

int compareStrings (const char s1[], const char s2[])
{
    int i = 0, answer;

    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 */

    return answer;
}

// Function to look up a word inside a dictionary

int lookup ( const struct entry dictionary[], const char search[],
             const int entries )
{
    int low = 0;
    int high = entries -1;
    int mid, result;
    int compareStrings (const char s1[], const char s2[]);

    while ( low <= high )
    {
        mid = (low + high) / 2;
        result = compareStrings (dictionary[mid].word, search);

        if ( result == -1 )
            low = mid + 1;
        else if ( result == 1 )
            high = mid - 1;
        else
            return mid;     /* found it */
    }

    return -1;              /* not found */
}

int main (void)
{
    const struct entry dictionary[100] = 
     {  { "aardvark", "a burrowing African mammal"        },
        { "abyss",    "a bottomless pit"                  },
        { "acumen",   "mentally sharp; keen"              },
        { "addle",    "to become confused"                },
        { "aerie",    "a high nest"                       },
        { "affix",    "to append; attach"                 },
        { "agar",     "a jelly made from seaweed"         },
        { "ahoy",     "a nautical call of greeting"       },
        { "aigrette", "an ornamental cluster of feathers" },
        { "ajar",     "partially opened"                  } };

    int   entries = 10;
    char  word[15];
    int   entry;
    int lookup ( const struct entry dictionary[], const char search[],
             const int entries );

    printf ("Enter word: ");
    scanf ("%14s", word);

    entry = lookup (dictionary, word, entries);

    if ( entry != -1 )
        printf ("%s\n", dictionary[entry].definition);
    else
        printf ("Sorry, the word %s is not in my dictionary.\n", word);

    return 0;
}

Upvotes: 0

Views: 637

Answers (2)

abhiarora
abhiarora

Reputation: 10430

The answer lies in the similarities between an array and a pointer.

The name of the array holds the address of the first element of the array. For example,

char arr[50];
char *ptr;

The name of the array in the above example is arr which holds the address of the first element of the character array ,i.e, &(arr[0]).The subscript operator with the array name acts as de-referencing operator,i.e,

arr[2] is similar to *(arr + 2)

So, the array act as a pointer but there is one major difference between array and pointers. The address to which array is pointing to, can't be changed but we can change the address being pointed by a pointer using assignment operator.

The normal functionality of a scanf function is to add an ampersand before the pointer to recognize the input as a pointer.

scanf() doesn't parse the argument list to recognize the type of arguments. Instead, we make use of reference operator & to get the address of the variable and pass it as an argument.

However, in this code example I found that the use of the ampersand sign was non-existent. Can anyone explain to me the reason why scanf allows word instead of &word? Thanks.

So, if you are passing word in the argument for scanf(). scanf() get the address of the first element of the array,i.e,

scanf("%s", word); is equivalent to scanf("%s", &(word[0]));

Since, word itself holds address to the first element, we don't need to use & operator to get the address.

Upvotes: 2

LeleDumbo
LeleDumbo

Reputation: 9340

In C, passing an array as argument to a function expecting pointer parameter equals to passing pointer to its first element. Hence:

scanf ("%14s", word);

equals:

scanf ("%14s", &word[0]);

Upvotes: 0

Related Questions