C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 30th, 2012, 10:28 AM
Alcorel Alcorel is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 Alcorel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 41 sec
Reputation Power: 0
Pointers: very basic question

Hey there,
I'm new to C and I just got started in pointers. In a given tutorial I get "segmentation fault: 11" when reproducing this simple code:


#include <stdio.h>
int main(){
int *n;
*n = 20;
printf("%i\n", *n);
return 0;
}

Why do I get "segmentation fault: 11" when I was supposed to get 20 as output?

Reply With Quote
  #2  
Old September 30th, 2012, 10:37 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
1) use code tags. Surround your code with [ code ] and [ /code ] (without the spaces)

2) you have a pointer: n. It is not initialized: you cannot safely put things where it points to (it doesn't point anywhere relevant) or look where it points.

Try this
Code:
#include <stdio.h>

int main(void) {
    int pointee = 42;
    int *n;
    n = &pointee; /* make n point to the pointee */
    printf("%d\n", pointee);
    printf("%d\n", *n);
    *n = 20;
    printf("%d\n", pointee);
    printf("%d\n", *n);
    return 0;
}

Reply With Quote
  #3  
Old September 30th, 2012, 11:09 AM
Alcorel Alcorel is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 2 Alcorel User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 41 sec
Reputation Power: 0
Thanks, your code worked fine. I tried doing something different and this time it worked.

Code:
#include <stdio.h>

int main(){

	// printing a pointer
	
	int *p;
	*p = 100;
	
	int n = 200;
	
	printf("Address [%p] = [%d]\n", p, *p);
	printf("Address [%p] = [%d]\n", &n, n);  
	
	return 0;
}



When I go back to my first example and changed to this it still gave me the segmentation fault:

Code:
#include <stdio.h>

int main(){

	/*
	POINTERS
	*/
	
	// *a (sets variable a as a pointer)
	// &b (returns the memory address of variable b)

	int *n;
	*n = 290;
	
	// printing the memory address of *n
	printf("Memory address: %p\n", n);
	printf("Value: %i", *n);
	
	/*
	// this will print 20
	printf("%i\n", *n);
	*/
	
	return 0;
}

Reply With Quote
  #4  
Old September 30th, 2012, 11:14 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Quote:
Originally Posted by Alcorel
I tried doing something different and this time it worked.


You got unlucky. It shouldn't have worked to make you notice the error: the pointer still points nowhere valid --- it hasn't been initialized or assigned a valid value.

You might like to read the comp.lang.c FAQ, particularly sections 4 and 5.

Reply With Quote
  #5  
Old September 30th, 2012, 12:38 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,139 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 8 m 12 sec
Reputation Power: 1974
As bdb told you, you just got very unlucky, because instead of crashing your program just clobbered a memory location instead, causing whatever good data that had been stored there to be replaced with garbage, thus corrupting your data. This corruption can very well give you very unexpected results that you are completely unable to understand how to correct. And when you do this in a larger project, it will be virtually impossible to troubleshoot and correct.

Never use an uninitialized variable, especially not a pointer!

Do you think that pointers are just some magical abstract idea? They are firmly rooted in the silicon lattices of your computer's hardware! Every single memory location that you store any data in resides in a memory chip. Locations within memory chips, and hence within your computer's memory space, are organized as sequential words, each of which has a unique address -- in most all personal computers, the size of those words is eight bits, a byte. Data larger than one byte uses multiple consecutive bytes. When your program is compiled, every variable is given a memory location whose address is associated with that variable, such that whenever your program accesses a variable's data for either reading or writing, it does so by using the address associated with that variable.

Pointers are simply indirect addressing which you learned in assembly class. Instead of loading a variable with data, you load it with the memory address where that data is located. The key phrase here is: "you load it with the memory address". You have to tell your program where that memory location is, which you do by initializing the pointer. If you don't, then it could just point anywhere and using it will result either in a segfault or in clobbered data.

Why don't you read your own comments?
Code:
	// *a (sets variable a as a pointer)
	// &b (returns the memory address of variable b)

	int *n;
	*n = 290;

To implement what the comments say:
Code:
    int  b;  // b is a variable; it has a memory location assigned to it
    int *a;   // a is a pointer; right now, it contains a garbage address and is unusable
    a = &b;    // a now contains the address of b and is now useable
    *a = 290;   //  now this is a safe operation, since a points to somewhere useable


In police, SWAT, and military training and even just when a civilian spends time on a firing range, one thing that is constantly emphasized is muzzle discipline. Always know where your firearm is pointing and that it is not pointing towards a friendly. The same applies to using pointers in C: always know where your pointers are pointing. Pointer discipline is absolutely essential. Otherwise you end up shooting yourself in the foot.
Comments on this post
bdb agrees: I loved the "muzzle disciplin" metaphor
MrFujin agrees: great explanation and I now learned the origin of "shooting your foot"

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Pointers: very basic question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap