Thread: Problem with changing variables :pointers, functions

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Problem with changing variables :pointers, functions

    Hi everyone, I'm trying to use a function and pointers to change 2 variables from nothing to whatever I scan in, When I compile the following code I get this:

    Warning: Assignment makes pointer from integer without a cast

    it happens at the *a = c; and *b = d; lines in the code below

    any help is apreciated

    Code:

    Code:
     
    include <stdio.h> 
    void scan(int **a, int **b)
    {
    	 int c, d;
     
    	 printf("Enter the size of matrix 1: ");
    	 scanf("%d %d", &c, &d);
     
    	 *a = c;
    	 *b = d;
    }
     
    main()
    {
    		int *row, *col;
     
    		scan(&row, &col);
     
    		 printf("%d %d, row, col);
    }

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Welcome to the boards!

    Make sure you know and understand your pointers, first off.


    The first big problem in your program is that you're using double pointers when you really don't need to. In your main function, you should change the row and col variables to just be normal integers and not pointers.

    Then, change your function to accept just a normal pointer instead of a double pointer and that's all you have to change.


    The reason for this is because in the following code:
    Code:
    *a = c;
    *b = d;
    You are assigning an int variable to an int* variable. This cannot be done (in this case).


    Also,
    Code:
    printf("%d %d, row, col);
    That shouldn't even compile. You need to have a quote after the second %d

    Enjoy.
    http://cslibrary.stanford.edu/104/

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The real problem is that they're trying to use scanf to scan into pointers that don't actually point at anything.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
    int c, d;
    printf("Enter the size of matrix 1: ");
    scanf("%d %d", &c, &d);
    Looks like they're pointing somewhere to me....It's the assigning that's causing the problem later on.

  5. #5
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Another problem is that *a and *b are both pointers since a and b are double pointers. So, whatever you assign to *a and *b is the ADDRESS of the variable that will be accessed with **a and **b. Therefore, when you say "*a =" and "*b =", the compiler is expecting an address after the equal signs. However, c and d are intergers, not addresses. If what you want is for **a to be the exact same value as c and for **b to be the exact same value as d, you need to say
    Code:
    *a = &c;
    *b = &d;
    That uses the address operator (&) on c and d, which tells the compiler to use the address of c and d, not the actual values of c and d. Here's kinda how the compiler is reading both *a = c and *b = d.
    Code:
    (pointer) = (interger)
    That's where the error "assignment makes pointer from interger without a cast" is coming from. You're assigning an interger value to a pointer value without a cast.

    A cast is basically somthing that turns one value into another type of value. If I wanted to use casting to fix that error (which you should note that you do NOT want to do in your particular situation) I would do this.
    Code:
    *a = (int*)c;
    *b = (int*)d;
    In that code, (int*) is the cast in both lines. And by putting it in front of c and d you are "casting" those interger values as pointers to an interger.

    Again, in your situation you would NOT want to do this. Let's say that c was 20 and d was 30. If you used this code
    Code:
    *a = (int*)c;
    *b = (int*)d;
    that would mean that when you used **a, the compiler would use the value stored at the memory address 20, which, 99.99999% of the time, will be memory that has absolutely nothing to do with the program you're writing. If you changed this piece of memory, you could get all kinds of different errors with your computer. The same goes for using **b. That means you're accessing the value stored at the memory address 30, which is most likely being used by some other application or program running on your computer.
    Last edited by Jaken Veina; 04-02-2005 at 06:05 PM.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    awesome

    thanks very much for all the replies, I see where I made my problem, there was no reason to declare the row and col variables as pointer, the function can change them if they are declared as regular integers, I guess I was trying to make it more complicated that it really is

    BTW pointers are so cool

    also thank you for the warm welcome, I can tell already I will be using these boards a lot in the furture =)
    ~Adam
    Last edited by SyCo1234; 04-02-2005 at 06:22 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jverkoey
    Code:
    int c, d;
    printf("Enter the size of matrix 1: ");
    scanf("%d %d", &c, &d);
    Looks like they're pointing somewhere to me....It's the assigning that's causing the problem later on.
    Yeah I misread that. I thought this:
    Code:
    main()
    {
    		int *row, *col;
     
    		scan(&row, &col);
     
    		 printf("%d %d, row, col);
    }
    Was this:
    Code:
    main()
    {
    		int *row, *col;
     
    		scanf("%d %d", &row, &col);
     
    		 printf("%d %d, row, col);
    }
    Doing too many things at once I guess. However, even if that was right, it's wrong. Because you'd be assigning pointers to local variables, which would expire when the function call ended. So either way it's wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    To further clarify things if you are still having some trouble. This is a simpler way of doing things by just using a pointer as an argument rather than a pointer to pointer:

    Code:
    #include <stdio.h>
    
    void scan(int*,int*);
    
    int main(int argc, char **argv)
    {
    	int row, col;
    	
    	scan(&row, &col);
    
    	printf("%d %d",row,col);
    
    	return 0;
    }	
    
    void scan(int* a, int* b)
    {
    	int c, d;
    
    	printf("Enter the size of matrix: ");
    	scanf("%d %d",&c, &d);
    
    	*a = c;
    	*b = d;
    }
    If you wanted to use a pointer-to-pointer as argument - which you don't need to in this case but heres how you would do it:

    Code:
    #include <stdio.h>
    
    void scan(int**,int**);
    
    int main(int argc, char **argv)
    {
    	int* row, *col;
    	
    	scan(&row, &col);
    
    	printf("%d %d",*row,*col);
    
    	return 0;
    }	
    
    void scan(int** a, int** b)
    {
    	int* c, *d;
    
    	printf("Enter the size of matrix: ");
    	scanf("%d %d",c, d);
    
    	*a = c;
    	*b = d;	
    }
    You only really need a pointer-to-pointer argument if you wanted to modify the value of a pointer in another function. In this case you only wanted to modify the value of an integer in another function.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by 0rion
    If you wanted to use a pointer-to-pointer as argument - which you don't need to in this case but heres how you would do it:

    Code:
    #include <stdio.h>
    
    void scan(int**,int**);
    
    int main(int argc, char **argv)
    {
    	int* row, *col;
    	
    	scan(&row, &col);
    
    	printf("%d %d",*row,*col);
    
    	return 0;
    }	
    
    void scan(int** a, int** b)
    {
    	int* c, *d;
    
    	printf("Enter the size of matrix: ");
    	scanf("%d %d",c, d);
    
    	*a = c;
    	*b = d;	
    }
    You only really need a pointer-to-pointer argument if you wanted to modify the value of a pointer in another function. In this case you only wanted to modify the value of an integer in another function.
    Um, except you'd be wrong here, because NOW you're calling scanf into pointers which don't actually point to anything. For real this time.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Hmm isn't that legal since both c and d are pointers to int? *shrugs*
    Compiled fine though

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're missing the point. Actually, the instance, but that wasn't very punny.

    Where are you storing the values you scan in? You aren't. You have pointers. But those pointers don't point to anything. Remember that pointers, like every other variable, store a value. The value for the pointers is the address of an actual variable instance. You don't have any, so you can't store an integeral value any place.

    It's like trying to put your clothes away and remembering you threw the drawers of your dresser away. You can't put them anywhere. You don't have anywhere to put them.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Hrm I kinda get what you mean (at least I think so!) but still dunnoe why it compiles and runs properly

    Could it be that it stores it in a random memory location since I didn't point the pointer to any specific address. And the *a and *b statements are pointing to that random memory address that has the pointer (c and d respectively) thus getting the values properly. But it could fail if the pointers randomly pointed to the same thing coincidentally but not in this case?
    Last edited by 0rion; 04-03-2005 at 12:48 AM.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. Uninitialized pointers, which is what you have, point some place randomly in memory. This means it can point to say, read only memory, or some other random spot in memory which you have no right to play with. What happens if it points to some spot in memory which is vital to the stability of your operating system, and you go screwing with the value of what's there?

    Usually however, you'll just get a segmentation fault and your program will crash.

    In short, your code is wrong. Will it compile? Sure, because the C language doesn't care if you point to any spot in memory. It's not the language's job to make sure you don't crash something. It's your job.

    You have no actual instance of any integers in your code. Furthermore, you don't dynamicly allocate any. You just have pointers. Pointers which point to some arbitrary point in memory, which you likely don't have any business tinkering with. You're lucky if all your program does is crash.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Thanks that clarified it a bit

    So something like this should suffice if you want to use a pointer-to-pointer?

    Code:
    void scan(int** a, int** b)
    {
    	int c, d;
    
    	printf("Enter the size of matrix: ");
    	scanf("%d %d",&c, &d);
    
    	*a = &c;
    	*b = &d;	
    }

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nope. That doesn't work either, as I already mentioned. There you're trying to assign the address of local variables, which are destroyed when the function ends. Try again.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to Class Member Functions
    By Dark_Phoenix in forum C++ Programming
    Replies: 6
    Last Post: 09-02-2007, 02:21 PM
  2. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 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