hey there, i need some help in understanding pointers. this is an example from school but i cant quite figure i out. i know the basis of pointers but could someone give me some more info.

#include <stdio.h>
#include <string.h>

int string_len(char *s);

int main() {
	char s[80];

	printf("length of %s is %d\n",s,string_len(s));
	return 0;

int string_len(char *s) {
	char *p;

	p = s;
	while (*s != '\0') {
	return s-p;

Also the use of pointers with structures and passing on arguments onto functions confusing me.

any tips on what to keep in mind when using pointers in general and when using structure pointers?.


edit: could someone please give me a simple example of pointers to structures and argument passing?

9 Years
Discussion Span
Last Post by csurfer

>i need some help in understanding pointers
I assume you are talking about the following?

int string_len(char *s) {
	char *p;

	p = s;
	while (*s != '\0') { // loop until the null-terminator (1)
	return s-p; // return the string's length (2)

(1) When this loop has finished, s points to the null terminator of the string.

(2) return s-p; : In the beginning of your function you let point p to 'first' character of the string (actually it is more correct to say: you let pointer p point to pointer s, this is the point from where you start counting)
So applying the rules of pointer arithmetic your function returns the length of the string :)

>Also the use of pointers with structures and passing on arguments onto functions confusing me.
For a complete reference on pointers I would recommend you to read this :)
(your requested examples are also covered here)


I just see that I posted using C++ code tags instead of C code tags, however it doesn't affect your code or something, I just wanted to let you know :)


Nice post there tux4life.

Just adding one more little information which is I know is very simple but just in order to complete the thread...:)

Every string manipulating function like strcpy strcmp etc take in the starting address of the string as input,so here as we are using character array to store the string we need to pass its starting address.
char name[20]'s starting address is address of name[0] which can be written as &name[0], but as you know for an array
&array[0] = array,
so we just pass in the name of the array when ever we need to pass its address.

And even when we write something like

printf("Hello there !!!");

Some memory is allocated internally for storing the string "Hello there !!!" and its starting address is passed on to printf.Therefore

printf("Hello there !!!");

is almost similar to

char array[20]="Hello there !!!";

OMG, the OP is answering his own question..... oh wait..... So you are twins !! That took me a while after noticing that you both share the same avatar.

> as you know for an array
> &array[0] = array,
> so we just pass in the name of the array when ever we need to pass its
> address.
Umm.. I am sensing a little trouble here. If you are cleared up on what is the difference between an array a pointer, it is fine, but if you are not: Arrays and pointers are different things in C (or C++). Read this won't harm.


@siddhant3s : Thanks for the wonderful article but I too have read the same :) !!!

I was just explaining the OP that in a condition such as

char name[20];

A compiler would treat "name" as nothing but "&name[0]" and would proceed.

Even though the printf example gives a warning when compiled in gcc 4.3 prints the string,and this in someway is helpful to get to the bottom of the concept.So this was mentioned.

And ya as a proof ;) I have mentioned a para from Same Article Section 16.Pointers down here :

The most frequent use of pointers in C is for walking efficiently along arrays. In fact, in the implementation of an array, the array name represents the address of the zeroth element of the array, so you can't use it on the left side of an expression. (You can't change the address of something by assigning to it.) If we say

char *y;
char x[100];

y is of type pointer to character (although it doesn't yet point anywhere). We can make y point to an element of x by either of

y = &x[0];
y = x;

Since x is the address of x[0] this is legal and consistent.

Hope now its ok with you :) and ya don't worry about the avtar !!! ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.