Thread: void *ptr problem

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    void *ptr problem

    hey guys it was my first time using void as parameter my code fails hardly though i dunno whats the problem in failing
    i m srry had problem in return code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define array_num 100
    int allocating_memory_func(void *stuff,int num,int num2)
    {
    	stuff=malloc(num2 * num);
    	if(stuff==NULL) 
    		return 0;//func failed
    	return 1;//function is cool
    	
    }
    int main(void)
    {
        int x;
        int *y;
    	if(!allocating_memory_func(y,array_num,sizeof x)){
    		free(y);
            puts("Function failed");
            getchar();
            exit(0);}
      else{
           for(x=0;x<array_num;x++){
            fputs("Please enter your num: ",stdout);
            scanf("%d",x[y]);}
        for(x=0;x<array_num;x++){
            printf("num %d is in array %d",x,x[y]);
    	}
    }
    	getchar();
        return getchar();
    }
    Last edited by lolguy; 04-08-2009 at 03:34 PM.

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    use sizeof on your call to malloc and also try to avoid the use of void pointers if theres another alternative.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your code fails because you cannot change what a pointer points AT unless you use a pointer to that pointer. Pointers are just like any other variable. They store a piece of information. In the case of pointers, that pointer is an addres.

    To change what a passed variable contains, so that it is reflected in the calling function, you have to have a pointer to what you want to change.

    If you want to change an int, you need a pointer to an int.
    If you want to change a pointer, you need a pointer to that pointer.

    Oh, and stop using fflush on input streams.


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

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i get msv6 this error
    C:\the test\main.c(16) : warning C4700: local variable 'y' used without having been initialized
    shouldnt alrdy y be intilised ?

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    but ptrs to void can be ptrs to anything srry for fflush i think its a habbit i need to get rid of it

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Where exactly are you initializing it?
    Quote Originally Posted by lolguy View Post
    but ptrs to void can be ptrs to anything
    Close, but not quite. But what does this have to do with anything?

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

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    then when its put to my function it will be alrdy intilised to int ?coz its a void ptr?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't initializing it before passing it to the function, so why would it be initialized once it arrives? If you aren't familiar with the term, "initalizing" means "providing a value". Like so:
    Code:
    int x;
    x = 5; /* here we are initializing x by giving it a value of 5 */
    Here we use it without initializing it.
    Code:
    int x;
    x++; /* increment x ... but wait, what value did it have before we added one to that? */

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

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    wouldnt it be go to the function and get intilised as stuff ?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. Otherwise your compiler wouldn't be yelling at you about it.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  3. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM