Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anything wrong with the pointer?

Tags:

I have a problem in comprehending the mechanism of some lines of code in *binsearch function. Particularly, low and high are two pointers and are initialized as &tab[0] and &tab[n] respectively. In the next line i see that low<high i think it invalid because it's impossible to compare two addresses of two pointers. The next line also have the same problem. I do not know whether I'm right or not and I need some ideas from you all.

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAXWORD 100
int getword(char *, int);
struct key *binsearch(char *, struct key *, int);
/* count C keywords; pointer version */
main()
{
    char word[MAXWORD];
    struct key *p;
    while (getword(word, MAXWORD) != EOF)
        if (isalpha(word[0]))
            if ((p=binsearch(word, keytab, NKEYS)) != NULL)
                p->count++;
    for (p = keytab; p < keytab + NKEYS; p++)
        if (p->count > 0)
            printf("%4d %s\n", p->count, p->word);
    return 0;
}
/* binsearch: find word in tab[0]...tab[n-1] */
struct key *binsearch(char *word, struck key *tab, int n)
{
    int cond;
    struct key *low = &tab[0];
    struct key *high = &tab[n];
    struct key *mid;
    while (low < high) {
        mid = low + (high-low) / 2;
        if ((cond = strcmp(word, mid->word)) < 0)
            high = mid;
        else if (cond > 0)
            low = mid + 1;
        else
            return mid;
    }
    return NULL;
}
like image 843
Hoang Nam Avatar asked Dec 20 '19 13:12

Hoang Nam


People also ask

What is the problem with pointers?

A memory corruption occurs when the program writes data to the wrong memory location, overwriting the data that was there, and failing to update the intended location of memory. Both of these problems falls squarely on the pointer. Though powerful tool, a pointer, can be a devil's advocate.

Why pointer is not secure?

Pointers are the notorious source of bugs, it makes the program less secured. So, java doesn't support pointers directly. But it is used internally in java. Java do not use pointers because using pointer the memory area can be directly accessed, which is a security issue.

Do people still use pointers?

Modern LanguagesThe underlying system behind many modern programming languages still uses pointers but they do not expose the complexity & accessibility of pointers to the programmers.

What is a common pointer error?

An invalid pointer reference occurs when a pointer's value is referenced even though the pointer doesn't point to a valid block. One way to create this error is to say p=q;, when q is uninitialized. The pointer p will then become uninitialized as well, and any reference to *p is an invalid pointer reference.


2 Answers

This is perfectly legal code. Excerpts from the standard (C11 draft)

6.5.8.5

When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript.

Also,

6.5.5.9

When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements.

In other words, it is legal to compare or subtract pointers pointing to elements within the same array (or one beyond the array). A subtraction yields the count of elements of the type being pointed to between the two pointers and a comparison provides information about which element is at a higher index in the array.

Now in the given code, the definition is:

struct key *low = &tab[0];
struct key *high = &tab[n];

Any comparison or subtraction between low and high or any other pointer of type struct key that points somewhere in-between them is legal.

like image 102
th33lf Avatar answered Sep 30 '22 19:09

th33lf


Two pointers can be compared if they are of the same array.

In particular these are the operation that you can do or not with pointers:

  • Pointer comparison is Valid only if the two pointers are Pointing to same array.

  • All Relational Operators can be used for comparing pointers of same type.

  • All Equality and Inequality Operators can be used with all Pointer types.

  • Pointers cannot be Divided or Multiplied.

So low < high is allowed and works well in this case.

like image 24
Zig Razor Avatar answered Sep 30 '22 21:09

Zig Razor