Solved

Char pointers

Posted on 2004-03-22
9
609 Views
Last Modified: 2010-04-15
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
Comment
Question by:XMan410
  • 3
  • 2
  • 2
  • +1
9 Comments
 

Author Comment

by:XMan410
ID: 10647620
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
 

Author Comment

by:XMan410
ID: 10647627
and..
I know nothing about the decleration: char (*ptr)[]; /* :-( */
How do you initialise it?
Can we use it later without initialising?
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10647758
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10648012
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 12

Expert Comment

by:stefan73
ID: 10648213
ssnkumar,
> ptr = (char (*) [])a;

That looks like a weird cast. Not much sense in that.
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10651342
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
 
LVL 8

Accepted Solution

by:
ssnkumar earned 80 total points
ID: 10654884
>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
 

Author Comment

by:XMan410
ID: 10656524
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
 
LVL 2

Assisted Solution

by:Avik77
Avik77 earned 20 total points
ID: 10659626
>>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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now