-4

So, I'm just practicing something in C language (I'm a beginner), but I'm now stuck on this program:

#include <stdio.h>
#include <string.h>

int main(){
    int numbers[12] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
    int i;
    int *point;

    point = numbers;

    while(i){

        printf("Write number in ranging 1 - 40: \n");
        scanf("%d", &numbers);

        if (numbers, point){
            printf("Your number is prime number.\n");
        }
        else if ((numbers <= 0) || (numbers >= 41)){
            printf("Only numbers: 1 - 40.\n");
        }
        else{
            printf("Try again.\n");
        }

        i;
    }
    return 0;
}

When I want to compile it, I just get this error message:

test.c: In function ‘main’:

test.c:19:38: warning: comparison between pointer and integer [enabled by default]

I looked around Stack Overflow, but I found nothing of similar topic and didn't help me.

Community
  • 1
  • 1
Yeez
  • 282
  • 1
  • 3
  • 9
  • 2
    possible duplicate of [comparison between pointer and integer \[enabled by default\]](http://stackoverflow.com/questions/26103409/comparison-between-pointer-and-integer-enabled-by-default) – ThisaruG Oct 01 '14 at 16:09
  • 4
    What do you think `if (numbers, point)` does? – Kevin Oct 01 '14 at 16:09
  • you really have to learn what a pointer is and how to use it. Clue: it's **not** an integer. – Paul Evans Oct 01 '14 at 16:11
  • I thought "if(numbers, point)" means - when I type some number from int array, it will print, that it's prime number. What are all in array. Ok, so I think, I should re-write it, without pointers, because it looks like, I failed with pointer. – Yeez Oct 01 '14 at 16:15
  • Learn some C basics and look at [this explanation of the comma operator](http://en.wikipedia.org/wiki/Comma_operator). – Jabberwocky Oct 01 '14 at 16:55

3 Answers3

1

Even if you're a beginner, you should still take the effort to tell us which lines are giving you errors, since it's not easy to tell exactly what line is number 19.

I think this line is giving you trouble.

if (numbers, point){    // bad

You probably meant to call some function that takes numbers and point as an argument. As it is right now, this if statement will always evaluate to true because you are using the comma operator, which just uses the value of the thing on its right, which happens to be a non-null pointer (point). How about something like:

if is_a_prime(numbers, point)

Moving on, the line that actually is causing the error is most likely this line:

 else if ((numbers <= 0) || (numbers >= 41)){    // bad

Since numbers is an array, it doesn't make much sense to write numbers <= 0. Perhaps you meant to write numbers[0] <= 0, or *point <= 0, or maybe you wanted to use a for loop to iterate over each number in the array to make sure it is positive.

Also, (thanks to isedev for seeing this), you never set i to any value before accessing it, so your program will have undefined behavior. Try writing something like i = 1; near the top.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • Not to mention `i` not being initialised and never being updated. And `scanf("%d",numbers)`... wow. – isedev Oct 01 '14 at 16:18
  • Looks like I did so many things wrong. I'll take a look on what you wrote and try to re-write it. And sorry, I forgot to put line with error, but you found it - "else if((numbers <= ..." – Yeez Oct 01 '14 at 16:26
  • @isedev Lol, good point, I don't understand how I could did it. /Facepalm – Yeez Oct 01 '14 at 16:27
0

First of all, this program will most likely not run. Specifically because you are saying while (i) when i is uninitialized (i.e garbage). It might be zero or any other value its initial address happens to point to.

Secondly, what do you mean by if (numbers, point) ? That doesn't mean anything.
Also, point is numbers, so there's no reason to compare them.

Thirdly, if you want to check for a prime number use the modulo operator. Like this.

Lastly, a pointer is not an integer or an array. When you say point = numbers that just stores the address of numbers in memory into point, and using pointer arithmetic (and because pointer[3] and array[3] mean the same thing ~ pointer + (3 * sizeof(datatype))), you might think that a pointer is an array, but it's not. Here's an example:

int h, *p;
h = 5;
*p = &h;
printf("*p: %d\n", *p);
h = 8;
printf("*p: %d\n", *p);

Here, *p is the value of h, and for a really simple reason.
p stores the address of h in memory, and when you say h = 5 the computer changes the value found at the address of h in memory. And because p happens to store the address itself in it, it can simply get the value found there. In short, p has no relation to h, dot.

Community
  • 1
  • 1
Amr Ayman
  • 1,129
  • 1
  • 8
  • 24
-1

There are a couple of problems in this code:

  1. Your i value variable is uninitialized
  2. If the garbage value of i is non zero its an infinite loop
  3. In the if condition you need to do a logical operation
  4. You are using scanf() not with pointer you use are using pointer to pointer
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Ginu Jacob
  • 1,588
  • 2
  • 19
  • 35