Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointers

Status
Not open for further replies.
May 3, 2002
633
US
Just for my own knowledge and something I wonder about is an explanation of the use of pointers used differently as shown:

const struct stack *
const char *s
struct record* prec

Could someone please explain all three, I am only used to using the second one in the brief uses of C that I use.

Thanks
 
The first one is a pointer to a data structure named stack.
The second one is a pointer to a char data type.
The last one is a pointer to a data structure named record.

The const keyword prevents the values being changed by your program.
 
from my perspective there aint no difference.
the pointer is a pointer to whatever is following the asterisk. I dont think the whitespace before or after the asterisk is attributing any difference.

const struct stack * ---> this is an error if nothing follows the asterisk.

const struct stack *StAck; --->correct

tomcruz.net
 
So in reality it doesn't matter where the * is placed when working with pointers? It can be either:

char *s
or
char * s
or
char* s

Is that correct?
 
> So in reality it doesn't matter where the * is placed when working with pointers?
Not in those three examples.

But bear in mind that
Code:
    char    *cp, c;
declares cp as a pointer to char, and c as a char

Code:
    char*    cp, c;
Might lead to the impression that both are pointers when they are not.

Some programming standards avoid this issue by not allowing such composite declarations in the first place.
 
"Some programming standards avoid this issue by not allowing such composite declarations in the first place."

But not C ;).

Aixspadmin,
The C standard doesn't have anything to say about what
how the asterisk(ptr notation)is aligned as long as its
otherwise a legal declaration.
That's left to pointy haired bosses and lead programmers.
 
Standards of Programming is not always just the language standard but can include many point haired boss and lead programmer things.

Code etiquette and style standards can both dictate that things that "look" wierd should be avoided. Why?

Because this:

#include <stdio.h>
char *cp = &quot;c&quot;;
char c = 'd';
int main(){
cp[0] = c;
printf(cp);
}

compiles to the same size as this:

#include <stdio.h>
char *cp,c;
int main(){ cp[0] = c; printf(cp); }

But the first one is easier to read. Please no smart remarks about lack of return, or the dangers of changing data in the way that I show. The sample is for style only.
 
So, what's your point?
That you can obfuscate C?
There's contests for that you know.
Alignment of an asterix isn't going a long way
in that direction.

Standards of Programming are offtopic in this forum
afaik.
 
char *cp, c;

this is careless as well.

separate the pointers from the char declarations.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top