LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2010, 05:38 AM   #1
ishandutta2007
LQ Newbie
 
Registered: May 2009
Location: kolkata,india
Distribution: fedora
Posts: 6

Rep: Reputation: 0
Explain the output(pointers)


explain me the output please .As far as my knowledge. a is a character type pointer variable and its size should be 4 bytes irrespective of what its pointing to.but i get a result =50.
*a is the value of a[0] and its size is rightly 1 byte.
but how can the size of that variable change to 4 byte when i just add 7 to it(although i don't store the added result anywhere)



Code:
#include<stdio.h>
int main(){
	char a[50];
	printf("\n%d  ",sizeof(a));
	printf("\n%d  ",sizeof(*a));
	printf("\n%d  ",sizeof(*a+7));
	return 0;
	}
output:
Code:
50
1
4

Last edited by ishandutta2007; 01-28-2010 at 05:42 AM.
 
Old 01-28-2010, 06:19 AM   #2
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by ishandutta2007 View Post
but i get a result =50.
"a" is an array of 50 chars, so size is correct.
Try this:
Code:
#include<stdio.h>
int main(){
	char a[50];
	char* b = a;
	printf("\n%d  ",sizeof(a));
	printf("\n%d  ",sizeof(b));
	return 0;
}

Last edited by SigTerm; 01-28-2010 at 09:09 AM.
 
Old 01-28-2010, 08:25 AM   #3
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
*a+7 means (*a)+7. And char+int=int.
 
1 members found this post helpful.
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
please explain the output of the printf dina3e Linux - Newbie 14 10-15-2008 11:03 AM
C pointers : What will be the output? duryodhan Programming 15 11-27-2006 11:40 PM
Could someone please explain this nmap output? stardotstar Linux - Networking 0 02-07-2005 10:02 PM
explain the output suchi_s Programming 9 11-08-2004 09:07 AM
explain the output c pragti Programming 10 05-28-2004 12:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration