Jump to content

C - Tracing where pointers point to in a program.

Go to solution Solved by Unimportant,
2 hours ago, GR412 said:

From my understanding a pointer is a variable whose value is a memory address of another variable. And that you declare a pointer like so: int *test; So i know what a pointer is and that it's used for passing by reference, but what confuses me is following where a pointer points to in a program. For example this program:


#include<stdio.h>

void up_case(char *c)
{
	if (*c>='a' && *c<='z')
		*c-=32;
}

void lo_case(char *c)
{
	if (*c>='A' && *c<='Z')
		*c+=32;
}

void traverse(char *s, void (*f) (char *))
{
	while (*s!='\0')
	{
		f(s);
		s++;
	}
}

int main()
{
	char str[]="CTEC2901 Data Structure and Algorithms";
	printf("Original: \'%s\'\n",str);
	traverse(str,up_case);
	printf("Upper Case: \'%s\'\n",str);
	traverse(str,lo_case);
	printf("Lower Case: \'%s\'\n",str);
	return 0;
}

I do understand this program, but what i don't get is what char *c in the up_case and lo_case function point to, as well as the char *s, void (*f) and char * in the traverse function. My guess is that char *c in the up_case and lo_case point to the value thats given in. The same with the traverse function char *s points to the char array called str, and the *f points is clearly a function pointer so it would point to either lo-case or up_case. 

 

Like I said i'm not really sure, so if someone could talk me through where each pointer points to in each function I'd be greatful.

 

In C, a array decays into a pointer, which means that in the lines:

traverse(str,lo_case);

//and...

traverse(str,up_case);

'str' becomes a 'char *' pointer to the first element in the array, thus it points to the first letter in the string

"CTEC2901 Data Structure and Algorithms". Given this, the traverse function works as follows:

void traverse(char *s, void (*f) (char *))
{
	while (*s!='\0')	//Keep looping as long as the character s is pointing to is not 0 (0 indicates end of string)
	{
		f(s); //Call the function pointed to by the function pointer, eighter lo_case or hi_case, and pass the pointer to it...
		s++;  //Increase the pointer by 1, making it point to the next character in the string.
	}
}

 

So it calls the lo_case or hi_case function for each letter in the string, passing a pointer to the current letter along so the function can work with that letter.

From my understanding a pointer is a variable whose value is a memory address of another variable. And that you declare a pointer like so: int *test; So i know what a pointer is and that it's used for passing by reference, but what confuses me is following where a pointer points to in a program. For example this program:

#include<stdio.h>

void up_case(char *c)
{
	if (*c>='a' && *c<='z')
		*c-=32;
}

void lo_case(char *c)
{
	if (*c>='A' && *c<='Z')
		*c+=32;
}

void traverse(char *s, void (*f) (char *))
{
	while (*s!='\0')
	{
		f(s);
		s++;
	}
}

int main()
{
	char str[]="CTEC2901 Data Structure and Algorithms";
	printf("Original: \'%s\'\n",str);
	traverse(str,up_case);
	printf("Upper Case: \'%s\'\n",str);
	traverse(str,lo_case);
	printf("Lower Case: \'%s\'\n",str);
	return 0;
}

I do understand this program, but what i don't get is what char *c in the up_case and lo_case function point to, as well as the char *s, void (*f) and char * in the traverse function. My guess is that char *c in the up_case and lo_case point to the value thats given in. The same with the traverse function char *s points to the char array called str, and the *f points is clearly a function pointer so it would point to either lo-case or up_case. 

 

Like I said i'm not really sure, so if someone could talk me through where each pointer points to in each function I'd be greatful.

 

   
   
Link to post
Share on other sites

in traverse function there is no "void (*f) and char *" because fl is one variable, f is a pointer that points on a function that argument is char pointer and the function has no return (void).

 

Another examples of function pointer would be

void (*anyname)() // anyname is a pointer to a functions that has no arguments and returns nothign
void (*p)(int a, double n) //p is a pointer to a function that takes two arguments, first int and second double and returns nothing.
void (*p)(int, double) //same as above but, so those names for argument variables arent necessary
int (*p)() //p is a pointer to a function that takes no arguments and returns int
int (*p)(int *, char *) //p is a pointer to a function that takes two arguments, int pointer and char pointer, and returns int

Usually there is & operator before variable you want pointer of. But function names and array names are pointers already so there is no & operator needed. But if you would like to point to single variable like a int, char, float, dobule or structure/class instance then you use &.

 

function tenless(int *a){
	a -= 10;
}

int main(){
	int i = 100;
	tenless(&i);
}

 

Last thing is that reference and pointer is not the same thing, when you have reference you work on it as on variable without * and -> operators.

Link to post
Share on other sites

If you want to know what the address is, just print out (or whatever) the pointer without a prefix. For example:

 

#include <stdio.h>
  
int main(){
  int *p;
  int foo = 123;
  p = &foo;
  
  printf("foo: %d, foo addr: %u, p defref: %d, p: %u", foo, &foo, *p, p);
  
  return 0;
}

Will print

foo: 123, foo addr: [whatever address foo has], p deref: 123, p: [whatever address foo has]

Unless you're working in bare metal or really old OSes, don't rely on the address of any variable to be the same for each run. :)

 

If you're using a debugger, it may be able to give the variable or function the pointer is pointing to, rather than the address.

Link to post
Share on other sites

1 hour ago, M.Yurizaki said:

If you want to know what the address is, just print out (or whatever) the pointer without a prefix. For example:

 


#include <stdio.h>
  
int main(){
  int *p;
  int foo = 123;
  p = &foo;
  
  printf("foo: %d, foo addr: %u, p defref: %d, p: %u", foo, &foo, *p, p);
  
  return 0;
}

Will print


foo: 123, foo addr: [whatever address foo has], p deref: 123, p: [whatever address foo has]

Unless you're working in bare metal or really old OSes, don't rely on the address of any variable to be the same for each run. :)

 

If you're using a debugger, it may be able to give the variable or function the pointer is pointing to, rather than the address.

Printing pointer values with %u is undefined behavior, printing memory addresses with printf requires the %p specifier:

 

printf("foo: %d, foo addr: %p, p defref: %d, p: %p", foo, &foo, *p, p);

 

Link to post
Share on other sites

2 hours ago, GR412 said:

From my understanding a pointer is a variable whose value is a memory address of another variable. And that you declare a pointer like so: int *test; So i know what a pointer is and that it's used for passing by reference, but what confuses me is following where a pointer points to in a program. For example this program:


#include<stdio.h>

void up_case(char *c)
{
	if (*c>='a' && *c<='z')
		*c-=32;
}

void lo_case(char *c)
{
	if (*c>='A' && *c<='Z')
		*c+=32;
}

void traverse(char *s, void (*f) (char *))
{
	while (*s!='\0')
	{
		f(s);
		s++;
	}
}

int main()
{
	char str[]="CTEC2901 Data Structure and Algorithms";
	printf("Original: \'%s\'\n",str);
	traverse(str,up_case);
	printf("Upper Case: \'%s\'\n",str);
	traverse(str,lo_case);
	printf("Lower Case: \'%s\'\n",str);
	return 0;
}

I do understand this program, but what i don't get is what char *c in the up_case and lo_case function point to, as well as the char *s, void (*f) and char * in the traverse function. My guess is that char *c in the up_case and lo_case point to the value thats given in. The same with the traverse function char *s points to the char array called str, and the *f points is clearly a function pointer so it would point to either lo-case or up_case. 

 

Like I said i'm not really sure, so if someone could talk me through where each pointer points to in each function I'd be greatful.

 

In C, a array decays into a pointer, which means that in the lines:

traverse(str,lo_case);

//and...

traverse(str,up_case);

'str' becomes a 'char *' pointer to the first element in the array, thus it points to the first letter in the string

"CTEC2901 Data Structure and Algorithms". Given this, the traverse function works as follows:

void traverse(char *s, void (*f) (char *))
{
	while (*s!='\0')	//Keep looping as long as the character s is pointing to is not 0 (0 indicates end of string)
	{
		f(s); //Call the function pointed to by the function pointer, eighter lo_case or hi_case, and pass the pointer to it...
		s++;  //Increase the pointer by 1, making it point to the next character in the string.
	}
}

 

So it calls the lo_case or hi_case function for each letter in the string, passing a pointer to the current letter along so the function can work with that letter.

Link to post
Share on other sites

On 26/10/2016 at 6:18 PM, Unimportant said:

In C, a array decays into a pointer, which means that in the lines:


traverse(str,lo_case);

//and...

traverse(str,up_case);

'str' becomes a 'char *' pointer to the first element in the array, thus it points to the first letter in the string

"CTEC2901 Data Structure and Algorithms". Given this, the traverse function works as follows:


void traverse(char *s, void (*f) (char *))
{
	while (*s!='\0')	//Keep looping as long as the character s is pointing to is not 0 (0 indicates end of string)
	{
		f(s); //Call the function pointed to by the function pointer, eighter lo_case or hi_case, and pass the pointer to it...
		s++;  //Increase the pointer by 1, making it point to the next character in the string.
	}
}

 

So it calls the lo_case or hi_case function for each letter in the string, passing a pointer to the current letter along so the function can work with that letter.

Would the s and f in the traverse function ( f(s) and s++; ) correspond to the memory address of the str array and function thats given in as argments?

 

   
   
Link to post
Share on other sites

10 minutes ago, GR412 said:

Would the s and f in the traverse function ( f(s) and s++; ) correspond to the memory address of the str array and function thats given in as argments?

 

 

Yes, f contains the memory address of the lo_case or hi_case function.

s contains the memory address of the first character in the str array when the function is entered. Each time "s++" is executed the pointer is increased by sizeof(char) to make it point to the next character in the array.

 

Link to post
Share on other sites

Just now, Unimportant said:

 

Yes, f contains the memory address of the lo_case or hi_case function.

s contains the memory address of the first character in the str array when the function is entered. Each time "s++" is executed the pointer is increased by sizeof(char) to make it point to the next character in the array.

 

Ahh yeha thats good, it's taken me about 2 weeks to understand all about pointers and how they're used to pass by reference. Thanks a lot for your replies they've helped.

   
   
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×