• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

Char pointers

A. What is the difference between:
char *ptr[];
and
char (*ptr)[]; /* I am particularly interested in this */
B.
How to initialise/assign values to each of the above?
0
XMan410
Asked:
XMan410
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
XMan410Author Commented:
What I know about *ptr[] is, that ptr can be used as an array of pointers; each pointer holding an address of a string. It has to be initialised only and cannot be assigned values without initialisation.
Ex:
char *ptr[]={"Experts","at","Experts Exchange","are","damn good!!"};

Now ptr is an array of 5 char pointers holding the addresses of the above strings.

Can we have a decleration: char *ptr[];
and use it later; if yes, how?
0
 
XMan410Author Commented:
and..
I know nothing about the decleration: char (*ptr)[]; /* :-( */
How do you initialise it?
Can we use it later without initialising?
0
 
stefan73Commented:
Hi XMan410,
> char *ptr[]={"Experts","at","Experts Exchange","are","damn good!!"};

> Now ptr is an array of 5 char pointers holding the addresses of the
> above strings.

Correct.

> Can we have a decleration: char *ptr[];

That syntax is only used in extern declaration and function arguments. There is no array of flexible size built into C.

Cheers,

Stefan
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ssnkumarCommented:
char (*ptr)[];  => ptr is a pointer to array of char.

Here is a sample program showing how it can be used:

#include <stdio.h>

main()
{
        char a[10] = "HELLO";
        char (*ptr)[];

        ptr = (char (*) [])a;

        printf("%s\n", ptr);
}

Hope it helps.....

-ssnkumar
0
 
stefan73Commented:
ssnkumar,
> ptr = (char (*) [])a;

That looks like a weird cast. Not much sense in that.
0
 
Avik DasguptaCommented:
Hi XMan410,
  the char *ptr[] has been explained very neatly I presume
It is the syntax of the formal parameter in command line arguments

int main(int argc, char *argv[]){}
where argv[0] is the program name, argv[1] is the first argument, ... so on (all are strings).

ssnkumar was correct, char (* ptr)[] is a pointer to an array of char;
Here is an illustration of how to use (modifying ssnkumar's code).

#include <stdio.h>

main()
{
        char a[10] = "HELLO";
        char (*ptr1)[],(*ptr2)[];
       
        ptr2 = "Hello World !"; // this will warn suspicious pointer conversion
        ptr1 = (char (*) [])a; // this won't
        printf("%s , %s\n", ptr1,ptr2);
}
The "wierd cast"  I presume was only to avoid that warning. Anyway u can always initialize any string by this method.
In a nut shell
----------------

char *ptr[] // array of pointers
initialized as
> char *ptr[]={"Experts","at","damn good!!"};
and accessed as ptr[0] is the string "Experts"

char (*ptr)[] // pointer to an array
initialized as
ptr="Hello World" ;// with warning OR
ptr=(char (*) [])"Hello World" ;// without warning

accessed as ptr as string or array of chars.

Avik.






0
 
ssnkumarCommented:
>That looks like a weird cast. Not much sense in that.
I think it makes sense only to the C compiler:-))
That's the only way you can initialize and cast to that pointer(char (*ptr)[];)
Here asterix (*) inside the parenthesis tells that it is a pointer and the next set of brackets tells that pointer is pointing to an array.
If you put asterix outside the parenthesis, then it becomes array of pointers (not a single array as in our "wierd" case)!
So, to tell the computer that, it is not an array of pointers but it is a pointer to an array and then assign the address of the actual array, you will have to do:
ptr = (char (*)[])a;

I don't know if my answer is convincing.......

Hope the questioner will appreciate the answer......

-ssnkumar
0
 
XMan410Author Commented:
yes ssnkumar, I appreciate that..
and yes, the warning message is avoided when I used that casting

but will you please elaborate..
1) What is the difference between a pointer to an array of characters and
the name of array, which itself is/(can be used as) the pointer to the base address.

                                   ptr,a are the variables in the example given by you.
2) I have tried using it in scanf:
scanf("%s",ptr); /* an warning is being given: possible use of ptr before definition.*/
/* anyway, i think we shouldn't do that without allocating memory. it is working once we allocate some memory to ptr */

3) how does it differ from:
scanf("%s",a);

4) a=ptr; is giving "lvalue required" as an error..
while,
 strcpy(a,ptr); is perfectly working.

XMan410
0
 
Avik DasguptaCommented:
>>a=ptr; is giving "lvalue required" as an error..

here 'a' is an array name
 and they cannot be modified. i.e any assignments or pointer arithmetics are illegal in case of an array. Since ptr is a pointer to a different address or null and definitely would not be assigned to an array. AN ARRAY IS LIKE A CONSTANT POINTER equivalent to the statement--
char * const a;

Moreover a %s expects a an array like char * const and definitely not a variable or uninitialized pointer, hence mismatch of expected data type gives the warning
>> scanf("%s",ptr); /* an warning is being given: possible >>use of ptr before definition.*/
It will also not take ur input.

1) Hence a pointer to an array
    char (*ptr)[];
2) Uninitialized pointer
    char * ptr;
3) An array
    char * const ptr;
    OR char ptr[10];
u can't change the pointer i.e of the base address of ptr[10];
but u can change it's value. That's why
>> strcpy(a,ptr)
  worked, because the pointer is not modified but only its value.
Hope I could make u understand.
Avik.



0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now