syntactic problem

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-23-2004
blue_gene
syntactic problem
it is a old code . i have a syntactic problem here...

Code:

```int n; char buf[81]; char (*lines)[81];  // what is the meaning of this  syntax ?  line -1 /* fopen fin and such */ n = 0; while ( fgets ( buf, sizeof buf, fin ) != NULL ) {   n++; } lines = malloc ( n * sizeof *lines ); rewind ( fin ); n = 0; while ( fgets ( buf, sizeof buf, fin ) != NULL ) {   strcpy ( lines[n++], buf ); }```

look at //line -1 . i have question on char (*lines)[81]; i dont understand the meaning of this syntax. what is the meaning of this ?

why there is a paranthesis ?

what is the alternate version of that ?

thanks
• 04-24-2004
Salem
char (*lines)[81];
lines is a pointer to an array of 81 chars

char *lines[81];
lines is an array of 81 pointers to char

The () are important here. The first one is just one pointer, whereas the second one is an array of 81 pointers.

What it basically allows you to do is create
char lines[n][81];
through the use of the malloc call later on in the code.
• 04-24-2004
blue_gene
Quote:

lines is a pointer to an array of 81 chars
ok.

one question more. what is happening here....

Code:

`lines = malloc ( n * sizeof *lines ); // how is this syntax ??`

i know sizeof(pointer) is legal. (basically gives 4 ..for 32 bit addressing)

but sizeof(*pointer) is it also legal !!!?? thats what has been done here...... dereferencing has been done inside the sizeof operator !

what does it mean if i write sizeof(*pointer) ?
• 04-24-2004
bennyandthejets
It dereferences the pointer, thus, it refers directly to the object it was previously pointing to. Thus sizeof(*pointer) will give you the size of the object it was pointing to. Eg:
Code:

```char chChar; char *lpChar=&chChar; cout << sizeof(lpChar); //output: 4 cout << sizeof(*lpChar); //output: 1```
• 04-24-2004
blue_gene
oh, ic...thanks.

char (*lines)[81];

can i write this alternatively like below.....

code-1
---------
char array[81];
char* lines = &array[0] // is it same as char (*lines)[81] ?

code-2
--------

char array[81];
char *lines = array; //is it same as char (*lines)[81] ?

can u coment plz ?

thanks
• 04-24-2004
bennyandthejets
Both of those will work. In the second example, 'array' resolves to a pointer to the actual array.
• 04-24-2004
blue_gene
ok thanks
• 04-24-2004
Prelude
>but sizeof(*pointer) is it also legal !!!??
Yes. sizeof doesn't evaluate its argument, so you get the size of the object being pointed to without actually dereferencing the pointer. If the dereference were evaluated then this would be illegal code as the pointer is probably pointing into limbo at the time.
• 04-24-2004
blue_gene
hi prelude, it was yours code.... i got it from a archieve.

Quote:

Yes. sizeof doesn't evaluate its argument, so you get the size of the object being pointed to without actually dereferencing the pointer. If the dereference were evaluated then this would be illegal code as the pointer is probably pointing into limbo at the time.
:confused:

former explanation was good to understand.

anyway, whether it really evalutes or not i understand what would be the output of that statement. :D

why r u saying it does not dereference. without dereference how thing will be output ?

the two coments are contradictory :confused:

at least i understand the output .

thanks for the reply
• 04-24-2004
Salem
Sizeof is entirely compile time (well until certain bits of C99 become common, but that's for later)
All it does is look at the type of the expression, there it nothing to evaluate

This leads to some quirky code if you're particularly careless...
Code:

```#include<stdio.h> int main() {     int foo = 0;     printf( "sizeof foo %d\n", sizeof(foo++) );     printf( "Foo is now %d\n", foo );     return 0; }```
This may or may not print foo as being 1

> char* lines = &array[0] // is it same as char (*lines)[81] ?
No
> char *lines = array; //is it same as char (*lines)[81] ?
No

You could do this, as a valid means of initialising
char array[81];
char (*lines)[81] = &array;

Or use the malloc call in your original post
• 04-24-2004
blue_gene
Quote:

This may or may not print foo as being 1
hmmm....

output
---------

g++ t.cpp
./a.out

sizeof foo 4
Foo is now 0
Quote:

there it nothing to evaluate
....i see.

Quote:

char array[81];
char (*lines)[81] = &array;
array name itself is a pointer . for example if i write array .
it means basically, &array[0] i.e the address of the first element of the array.

but u r writing, char (*lines)[81] = &array; it means if i expand this mathematically it will be like this....

char (*lines)[81] = &array;
=>char (*lines)[81] = &(&array[0]); // just replacing

RHS is a pointer to pointer !! LHS is a single pointer .

sorry , for generating confusuion, but it is coming to mind. somewhere something might be wrong in my thinking.

thanks
• 04-24-2004
Salem
> RHS is a pointer to pointer !! LHS is a single pointer .
No it isn't - what you wrote doesn't make any sense.
&array is a distinct type from array and &array[0]

&array is a pointer to the whole array, which happens to start at the same place as &array[0]

Consider an alternative example using a struct.
Code:

```#include<stdio.h> struct foo {     int    bar;     char    name[100]; }; int main() {     struct foo foovar;     struct foo *fooptr = &foovar;      /* point to the whole thing */     int        *barptr = &foovar.bar;  /* point to a part of the whole */     printf( "%p %p\n", (void*)fooptr, (void*)barptr );     return 0; }```
fooptr is like &array
barptr is like &array[0]

Maybe this will help
http://pw2.netcom.com/~tjensen/ptr/cpoint.htm
• 04-24-2004
blue_gene
hi salem..i tested the code.

output
-----------
g++ t.cpp
./a.out
0xbfffe560 0xbfffe560 // same address

thanks for that solid code. your answers are always to the point .....thanks for correcting me.... i have read about pointers. still, i confuse sometimes. people say, poiners are the difficult section of C/C++ programming.

anyway, thanks.....i learned a lot.
• 04-24-2004
bennyandthejets
Quote:

No
> char *lines = array; //is it same as char (*lines)[81] ?
No
Why isn't it the same?
Code:

```char array[81]; char *lines=array;```
lines is a pointer to the first of 81 sequential chars. How does it differ from char (*lines)[81]?
• 04-24-2004
Hammer
>char (*lines)[81]?
This is a pointer to an array of 81 chars.
>char *line;
This is a pointer to a single char.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last