Thread: Invalid operands to binary *

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    11

    Question Invalid operands to binary *

    This is probably a pretty simple problem to fix, but it is causing me a great deal of concern. The problem appears in the line
    Code:
    spot = (point+place)*y;
    Where spot is an integer pointer, point is another integer pointer, place is an integer, as is y. I definitely need to add place and point before multiplication, but it just wont accept my ideas. Much like society at large...

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    If spot and point are pointers, you need to dereference them before doing your calculations with them.

    The actual value of spot is a memory address like 00430030.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int *pointer = malloc(sizeof(*pointer));
    
    	*pointer = 5;
    
    	printf("pointer = &#37;d\n", pointer);
    	printf("*pointer = %d\n", *pointer);
    	
    	return 0;
    }
    Output:
    Code:
    pointer = 4390960
    *pointer = 5
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    11
    I'm not sure if that's going to work for my particular problem
    point is the beginning of a dynamically allocated array, and as such I don't really want to change it, or its contents.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by jrfall349 View Post
    I'm not sure if that's going to work for my particular problem
    point is the beginning of a dynamically allocated array, and as such I don't really want to change it, or its contents.
    You cannot add pointer to pointer. You can add int to pointer to get a new address or add value stored in the pointed location to another value. to do so - you should dereference your pointer:

    Code:
    *point + *place
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Dereferencing by itself does not change anything. It just accesses the data that the pointer points to. And if point is an array then wouldn't you want to be accessing elements of that array rather than the memory address? Like:
    Code:
    point[i]
    Don't quote me on that... ...seriously

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    11
    Guess I'm just not sure what dereferencing is actually doing. We haven't covered that yet.

  7. #7
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    Memory
    Address		Value
    ==========================
    		|--------|
    0x458700	|0x458704| <- pretend this is the pointer
    		|--------|
    0x458704	|00000015|
    		|--------|
    0x458708	|00010555|
    		|--------|
    So then if you have a pointer, it's value may be 0x458704. int *pointer; pointer would be 0x45804. If you do something to it like pointer++, pointer would be 0x45808. That's not what you want. You want to change the value that pointer is pointing to, 15 in this case. '*' dereferences the pointer. *pointer is equal to 15 because 15 is the value stored at memory address 0x458704. (*pointer)++ would change the 15 to 16. The value at memory address 0x458704, would now be 16. The pointer is still equal to 0x458704.
    Don't quote me on that... ...seriously

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    11
    Okay, I think I get it... I don't want to add to point, I want to add to the value of point...
    Only problem I get now is

    [Warning] assignment makes pointer from integer without a cast

    And my program crashes. Urg this thing's starting to bug me.

  9. #9
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    If the code isn't extremely large, post it all.
    Don't quote me on that... ...seriously

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: invalid operands to binary *
    By cblix in forum C Programming
    Replies: 5
    Last Post: 01-29-2006, 08:45 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Need help with C program
    By ChrisH in forum C Programming
    Replies: 38
    Last Post: 11-13-2004, 01:11 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM