The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
far pointer useless79 High Level Programming 1 11-08-2007 01:13 AM
A Pointer to non-Virtual Address, and All of my Hard drive xcoder66 High Level Programming 9 12-19-2005 01:22 PM
pointer sarwan High Level Programming 1 11-15-2005 02:41 AM
How to Achive IP address through MAC(Ethernet) address krishnacins IP Networking 3 08-29-2005 05:45 PM
network address and broadcast address? pnxi UNIX for Dummies Questions & Answers 7 11-10-2003 07:29 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-15-2006
Registered User
 

Join Date: Jul 2006
Posts: 25
Stumble this Post!
address of pointer

Hi i'm new to c programming and i'm trying to change the address of a pointer/variable but i can't seem to get it right,

I have this

Code:
char heap[ 134 ];
char *firstFree = heap;
char *allocMem( int size ) {
        void *malloc(size_t sizeofint);
        /*allocate space for an array with size number of elements of type int*/
        int * ip = malloc(sizeof(int) * size);
        if(ip == '\0'){ /*If function is null, memory not allocated so return null*/
            return '\0';
        }else{ /*Otherwise return address of first free space*/
            firstFree = *ip+1;
            return firstFree;
        }
The bit in bold is wrong, I'm not sure how to change the adress of firstFree to be whatever ip is, any suggestions?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-15-2006
Registered User
 

Join Date: May 2006
Posts: 10
Stumble this Post!
firstFree = *ip+1;

the "*" is not needed, and the pointer type is different
Reply With Quote
  #3 (permalink)  
Old 08-15-2006
Registered User
 

Join Date: Jul 2006
Posts: 25
Stumble this Post!
warning: assignment from incompatible pointer type

What should i do?
Reply With Quote
  #4 (permalink)  
Old 08-15-2006
Supporter
 

Join Date: Jul 2006
Posts: 156
Stumble this Post!
This warning means that the pointer types are different.

'ip' is a pointer to an integer. 'firstFree' is a pointer to a character. Change
Code:
        int * ip = malloc(sizeof(int) * size);
to
Code:
        char * ip = malloc(sizeof(int) * size);
Reply With Quote
  #5 (permalink)  
Old 08-15-2006
Registered User
 

Join Date: Jul 2006
Posts: 25
Stumble this Post!
Thanks nathan, it worked, but now I'm trying to print all the content of the memory and this isn't printing it, I don't see where I'm going wrong

Code:
    char letter;
    int counter = 0;
    int i;
    for (i=0; i < *firstFree; i++){
        letter = heap[i];
        printf ("%d   ",(char) letter);
        counter++;
        if(counter==10){
            counter = 0;
            printf("\n");
            }
    }
Reply With Quote
  #6 (permalink)  
Old 08-15-2006
Registered User
 

Join Date: May 2006
Posts: 10
Stumble this Post!
for (i=0; i < *firstFree; i++)

*firstFree ,what does it mean?
i think you want to get the length of the string that allocated
isn't it? it is wrong if so
Reply With Quote
  #7 (permalink)  
Old 08-15-2006
Registered User
 

Join Date: Jul 2006
Posts: 25
Stumble this Post!
no not the string length but the address of the firstFree (like the heap[] address 0, 1, 2 etc ...)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0