Thread: How to assign value to the pointer in pointers to an array

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Question How to assign value to the pointer in pointers to an array

    Hi,

    i have code like this poinetrs to 'char' array.

    char name[10];

    char (*p) [10];

    now i want to show the entered value for array 'name' to *p .
    tried but
    error 'type incompatible with assignment operator'
    with,

    *p = name;
    *p = 'd';
    *p = name[1];

    can you giude me regarding this pointer to char array.

    thank you.

  2. #2
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    char* p[10]
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yeah, you syntax is very wrong. First, why did you make an array of pointers? Secondly, and the reason it's having problems, is you're not supposed to have any unary operations on the lValue. You don't need any for assigning arrays to pointers anyway, but if for instance you were assigning a regular data type, then:

    Code:
    // *p = integer1 should be
    p = &integer1;
    but as I said, in the case of arrays, you're assigning the address of the array by default so you don't need the address of operator.

    Code:
    #include <stdio.h>
    
    int main() {
        char foo[13] = "Hello World!";
        char *bar;
        
        bar = foo;
        
        printf("%s", bar);
    }
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by SlyMaelstrom
    ... First, why did you make an array of pointers?:
    It's actually a pointer to an array of chars, unless you're refering to Roshanx's example, which is an array of pointers - to char.

    amas, I don't fully get your question but by the "*p" syntax use, it looks like all you really need is a character pointer: char* p = name;


    xeddiex.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ah yes, I see. Well in your case, then, the only thing you would need to make it run is the address of operator.

    Code:
    p = &name;
    Though, I've never seen that syntax. The example I cited earlier is much more common.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    135

    Wink

    Quote Originally Posted by SlyMaelstrom
    Ah yes, I see. Well in your case, then, the only thing you would need to make it run is the address of operator.

    Code:
    p = &name;
    Though, I've never seen that syntax. The example I cited earlier is much more common.
    Not really, just the array name without the operator is fine because arrays automatically expose their address. I think you said this yourself in your first post .. hmm


    xeddiex.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by xeddiex
    Not really, just the array name without the operator is fine because arrays automatically expose their address. I think you said this yourself in your first post .. hmm

    xeddiex.
    Sly is correct. It is a matter of type.
    Code:
    char foo[10];
    
    foo   // yields a pointer to character - char*
    &foo // yields a pointer to a character array [10]  - char (*) [10];
    As Sly said, the second syntax is very rare. There are very few uses for it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to multidimensional arrays
    By Luciferek in forum C++ Programming
    Replies: 37
    Last Post: 10-02-2008, 12:57 PM
  2. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  5. Dynamic pointer array in C
    By MacFromOK in forum Windows Programming
    Replies: 14
    Last Post: 04-09-2005, 06:14 AM