PDA

View Full Version : What do the *s mean in C?



microfi
March 23rd, 2008, 11:38 PM
Hi folks!
I'm a C-programming newbie and I'm having a few doubts with the code generated by Anjuta (when you create a new project):
What does the * characters mean (pardon my question, that's because I don't know the name of this character in English, so I was unable to search for it on Google) in situations like these:


GtkWidget *window;
GladeXML *gxml;

What's different if I do this?


GtkWidget window;
GladeXML gxml;

And in functions over the reference manuals they appear too:

void gtk_signal_connect_while_alive (GtkObject *object, const gchar *name, GtkSignalFunc func, gpointer func_data, GtkObject *alive_object);

What do they mean?
Thanks, Filipe

slavik
March 23rd, 2008, 11:47 PM
read up on pointers. :)

SledgeHammer_999
March 23rd, 2008, 11:48 PM
these are pointers. Read about them here (http://cplusplus.com/doc/tutorial/pointers.html). The link refers to C++ but the same things apply to C(at least for pointers).

EDIT:'*' is called "asterisk" in english.

Lster
March 23rd, 2008, 11:51 PM
SledgeHammer_999's link is probably clearer but you may also find this useful:

http://en.wikibooks.org/wiki/C_Programming/Pointers_and_arrays

microfi
March 23rd, 2008, 11:52 PM
Thanks folks!
The thing is that I'm brazilian and it's called "asterisco" here... Not so different...
I'll read something about pointers!
:) Filipe

Joeb454
March 23rd, 2008, 11:56 PM
Make sure you at least know the basic's first ;)

When you start venturing into Pointer's and Dynamic Memory allocation (I've even done pointers to arrays of pointers) It starts to get very confusing :p

microfi
March 24th, 2008, 12:18 AM
I'm new to C, but I built programs with Delphi in Windows.
Should I consider using other languages such as Python first?

slavik
March 24th, 2008, 12:30 AM
I'm new to C, but I built programs with Delphi in Windows.
Should I consider using other languages such as Python first?
depends ... IMO, C is a good first language because it is small.

once you understand pointers, you have pretty much understood the most difficult concept C has to offer (there are programming concepts of recusion that for some reason give people trouble).

Python as a language (along with Java, Perl, C++, C#, Ruby) is huge.

Although there are people who say that Python is better as a first language, but I disagree with them.

LaRoza
March 24th, 2008, 12:32 AM
depends ... IMO, C is a good first language because it is small.

once you understand pointers, you have pretty much understood the most difficult concept C has to offer (there are programming concepts of recusion that for some reason give people trouble).

Python as a language (along with Java, Perl, C++, C#, Ruby) is huge.

Although there are people who say that Python is better as a first language, but I disagree with them.

I agree with both parties. You move slower in C, however, so don't be too hard on yourself if you don't get it at first.

Can+~
March 24th, 2008, 01:41 AM
You should finish learning C, understand pointers, structures, how to do things with strings, and then start with another language, such as (*edited because of someone ranting*), java, python, perl, etc.

About the subject, pointers are references to other objects, for instance, when you do a function:


void sum(int *a, int b){
*a += b;
}

int x = 5;
sum(&x, 4);
printf("result: %d", x);

that will cause the variable x to be 9 (5+4), since I passed the reference to the variable, thus, making the content on that variable change, as opposed to:

void sum(int a, int b){
a += b;
}

int x = 5;
sum(x, 4);
printf("result: %d", x);
this last one will not modify the variable x. The function will be called and the value be copied into the function. When using this type of functions, you must return a value so it makes sense. Example with return:


void sum(int a, int b){
a += b;
return a;
}

int x = 5;
x = sum(x, 4);
printf("result: %d", x);

Now it's equivalent to the first one, x is 9 again.

*Important stuff:
I forgot to tell you, there's another things you should remember:
-<type> *a makes a pointer of the type <type>.
-*a access the content to the pointer

If I got a variable already, like:
int b;
-&b will return the pointer of b
-b will return the value of b

And when you create a string:
char b[50];
-b is the pointer to the first element on the string, thus, you don't need to do &b when passing it.

So a simple example:

int *a; // Pointer a
int b=4; // Normal integer b
int c=0; // Normal integer b

a = &b; // Pass the pointer of b to a.
c = *a; // hands the value contained in b, pointed by a.

Hope that makes things clear.

*Disclaimer: If I made a mistake above, is probably due to my extensive sessions with python, that have screwed my C syntax.

bruce89
March 24th, 2008, 02:19 AM
You should finish learning C, understand pointers, structures, how to do things with strings, and then start with another language, such as python.

*deleted rant about why people say "Use Python, not $LANGUAGE_THE_OP_IS_ASKING_ABOUT*

LaRoza
March 24th, 2008, 02:30 AM
*deleted rant about why people say "Use Python, not $LANGUAGE_THE_OP_IS_ASKING_ABOUT*

If someone is asking such a basic question, it shows a lack of study in the subject.

It isn't asking what pointers are, their uses, or a better explanation, it is asking a rather...silly thing. Any tutorial or book on C, which contains pointers in the code examples will have an explanation of what they mean to some degree.

Higher level languages allow one to worry less above how computers work internally, and allow one to to focus on the problem.

Can+~
March 24th, 2008, 03:00 AM
*deleted rant about why people say "Use Python, not $LANGUAGE_THE_OP_IS_ASKING_ABOUT*

I didn't just send him to python. I answered the whole question, and gave examples on how to use C pointers.

I gave the idea of using python (or any higher level language), since C is a pretty short language in terms of what a beginner can do, including the standard libraries.

C is usually as to understand the basis of other languages, unless he wants to develop a kernel, or build applications on C (in which case, I would recommend objective C), anyone should jump into higher level, and since everyone says python is great and friendly, I thought it would be a good start.

Are you happy now?

Jessehk
March 24th, 2008, 04:57 AM
I love this (http://www.eternallyconfuzzled.com/tuts/languages/jsw_tut_pointers.aspx) tutorial on pointers. It is extremely thorough and comprehensive.

wozzinator
March 24th, 2008, 05:19 AM
Hi folks!
I'm a C-programming newbie and I'm having a few doubts with the code generated by Anjuta (when you create a new project):
What does the * characters mean (pardon my question, that's because I don't know the name of this character in English, so I was unable to search for it on Google) in situations like these:


What's different if I do this?


And in functions over the reference manuals they appear too:


What do they mean?
Thanks, Filipe

There are plenty of posts above mine with links to things about pointers, but I figure I can give you the quick summary:

Any variable with a * in front of the name means that its the actual value stored in the memory location of that variable.

On the other hand any variable with a & in front of the name means that its the memory address of that variable.

rye_
March 24th, 2008, 01:59 PM
funnily enough when I saw *s instead of just * in the total post my first thought was; what! c with regular expressions!

nvteighen
March 24th, 2008, 03:46 PM
Just a little thought: Aren't pointers just like symlinks? If they're so, why people get so confused with them?

ruy_lopez
March 24th, 2008, 03:57 PM
Just a little thought: Aren't pointers just like symlinks? If they're so, why people get so confused with them?

In theory you're right. Why do people get confused? Because in practice they're nothing like symlinks.

Using pointers is an arcane subject. You can learn pointers easily enough. But when you start using them, they have a tendency to blow up in your face.

I've been using them for years, and I still get caught out. It'll be a long while before I profess to know everything about pointers.

EDIT: I feel compelled to add this, so I don't give the impression pointers are difficult. Expertise for about 90% of pointer usage can be learned quite quickly. It's the other 10% that takes time, and is mysterious to mere mortals (and that includes me).

LaRoza
March 24th, 2008, 04:49 PM
Just a little thought: Aren't pointers just like symlinks? If they're so, why people get so confused with them?

Pointers are simple concepts, but really require knowledge of assembly to really understand. They are a "low level" feature in that they require one to know how a computer works.

nvteighen
March 24th, 2008, 05:08 PM
Pointers are simple concepts, but really require knowledge of assembly to really understand. They are a "low level" feature in that they require one to know how a computer works.

Funny, because I'm just starting with some C (wanted to know and well... became fascinated by it) and my whole little programming experience is at "high level" languages.

Maybe people try to use them before they really understand what they are and get stuck.

CptPicard
March 24th, 2008, 05:32 PM
Pointers are simple concepts, but really require knowledge of assembly to really understand. They are a "low level" feature in that they require one to know how a computer works.

I would say that what you need to understand in order to understand why you need pointers is the idea of passing by value. This brings you to the concept of the call stack. You need to be able to put some kind of a "reference" to an item on the stack instead of the item itself if you want to modify the referent in the function call. It's not quite necessary to go to the implementation details (addresses etc).

slavik
March 24th, 2008, 07:47 PM
Pointers are simple concepts, but really require knowledge of assembly to really understand. They are a "low level" feature in that they require one to know how a computer works.
knowing how addresses work does require low level knowledge.

instead of me giving you a box of stuff, I can a give you a box with the locker number where the box is of stuff is. :)

simple analogy, simple concept, powerful (aka dangerous) usage. :)

Cows
March 24th, 2008, 08:06 PM
I didn't know C had pointers. I'm a C++ programmer and I thought that only C++ had pointers and classes over C's structures. I guess thats something new :).

CptPicard
March 24th, 2008, 08:09 PM
I thought that only C++ had pointers and classes over C's structures.

:lolflag: n00b. ;)

LaRoza
March 24th, 2008, 08:49 PM
I didn't know C had pointers. I'm a C++ programmer and I thought that only C++ had pointers and classes over C's structures. I guess thats something new :).

Well, no offense, you should learn another language other than Blub.

It is understandable to be ill informed when a beginner, so I am not critizising you, just that that statement is slightly humourous.

Cows
March 24th, 2008, 10:39 PM
I have been programming for a few years actually. I haven't taken it seriously and I have been looking for the language that I feel comfortable with. Anyways I have found that after slowing down development with C++ , and starting with Objective-C, that C++ is actually the language the I am most experience with and comfortable as well. Even tho I have been using C++ for quite a while, I still consider myself a noob.

What happens is that as you get older you start experimenting with what you want to do for your career. I'm still looking for my thing but I'm almost there. It's either Computer Science, Graphic Designing or IT Tech.

LaRoza
March 24th, 2008, 10:56 PM
I have been programming for a few years actually. I haven't taken it seriously and I have been looking for the language that I feel comfortable with. Anyways I have found that after slowing down development with C++ , and starting with Objective-C, that C++ is actually the language the I am most experience with and comfortable as well. Even tho I have been using C++ for quite a while, I still consider myself a noob.

What happens is that as you get older you start experimenting with what you want to do for your career. I'm still looking for my thing but I'm almost there. It's either Computer Science, Graphic Designing or IT Tech.

Good luck! Learning is always good.

Objective-C is a strict super set of C (in that C is valid Objective-C). Did you see pointers in that? Pointers are not normally used in C++ (at least, not naked pointers)

ruy_lopez
March 24th, 2008, 11:28 PM
Good luck! Learning is always good.

Objective-C is a strict super set of C (in that C is valid Objective-C). Did you see pointers in that? Pointers are not normally used in C++ (at least, not naked pointers)

It's difficult to use obj-c without using pointers. That's how objects are created.

LaRoza
March 24th, 2008, 11:50 PM
It's difficult to use obj-c without using pointers. That's how objects are created.

The poster stated it was a while ago, so it is understandable the subject isn't the most retentive.

ruy_lopez
March 24th, 2008, 11:59 PM
The poster stated it was a while ago, so it is understandable the subject isn't the most retentive.

I was being facetious. You are right. In obj-c, pointers and objects are so inseparable, you could use obj-c for years without even realising you are dealing with pointers.

Cows
March 25th, 2008, 12:13 AM
Good luck! Learning is always good.

Objective-C is a strict super set of C (in that C is valid Objective-C). Did you see pointers in that? Pointers are not normally used in C++ (at least, not naked pointers)

Yeah, I've seen the pointers in Obj-C. Plainly to start a C or C++ main function you would use:


int main( int argc, char * argv[] );