Thread: Allocation of Memory in Heap Error

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    3

    Arrow Allocation of Memory in Heap Error

    Not sure what is the error in this code. This is from the book called "Understanding and Using C Pointers". This is from page number 36. It is supposed to demonstrate that we allocate 6 bytes in memory bt we use 8 bytes.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        char *pc = (char*) malloc(6);
    
        for(int i=0; i<8; i++) {
            *pc[i] = 0;
         }
    }
    However, when I run it, I get the following error.

    error: invalid type argument of unary '*' (have 'int')

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    Change *pc[i] to pc[i] as pc is a pointer to char, so pc[i] is a char, and you cannot dereference a char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2019
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Change *pc[i] to pc[i] as pc is a pointer to char, so pc[i] is a char, and you cannot dereference a char.
    Thanks for the response.
    When you say we can't dereference a char, why not?
    I am kind of new to pointers.
    But the in the following code snippet I can dereference a pointer, or am I missing something.

    Code:
    int main() {
        char c = 'a';
        char *pc = &c;
        printf("%c\n", *pc);
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    Quote Originally Posted by josnow
    When you say we can't dereference a char, why not?
    A char isn't a pointer.

    Quote Originally Posted by josnow
    But the in the following code snippet I can dereference a pointer, or am I missing something.
    That just proves that you can dereference a pointer to char. It doesn't prove that you can dereference a char. Try to compile this:
    Code:
    int main() {
        char c = 'a';
        printf("%c\n", *c);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2019
    Posts
    3
    OK, I got it.
    Thanks again.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    550
    Quote Originally Posted by josnow View Post
    When you say we can't dereference a char, why not?
    I am kind of new to pointers.
    Think of pointers as any normal variable, but instead of containing a "value" it contains a memory address.
    The declaration:
    Code:
    char *p;
    Declares a variable called 'p' (not '*p') of 'char *' type. So 'p' will store a memory address.
    "Dereference" is the operation to use the "reference" (the pointer variable) as a memory address and get data 'pointed' by it.
    In a crude way:
    Code:
    char *p; // declare p as a pointer to a char.
    p = 0x6001a3;  // Puts the address 0x6001a3 in p.
    *p = 'a';  // puts the character 'a' in the address contained in p.
    But, of course, instead of initializing 'p' with a literal address you'll get the "address of" another symbols, for example:
    Code:
    char s[] = "fred";
    char *p;
    
    p = &s[1]; // p is assigned with the 'address of' s[1].
    putchar( *p ); // will print 'r'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A memory allocation error
    By zahid990170 in forum C Programming
    Replies: 1
    Last Post: 05-02-2011, 06:26 AM
  2. MAX memory allocation from c runtime heap
    By sandylovesgnr in forum C Programming
    Replies: 2
    Last Post: 10-23-2008, 11:02 PM
  3. memory allocation from stack and heap ?
    By Bargi in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 12:37 AM
  4. stack vs heap memory allocation
    By gongchengshi in forum C Programming
    Replies: 9
    Last Post: 11-18-2007, 12:17 PM
  5. HELP!!! Memory allocation error!
    By killiansman in forum C Programming
    Replies: 6
    Last Post: 06-08-2007, 11:47 AM

Tags for this Thread