473,776 Members | 1,503 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's the position of pointers

When I began to learn C, My teacher told me that pointer is the most
difficult part of C, it makes me afraid of it. After finishing C
program class, I found that all the code I wrote in C contains little
pointers, obviously I avoid using them.
A few days ago when I was reading a book about programming, I was told
that pointers are the very essence of C language, if I couldn't use it
well, I'm a bad programmer, it's a big shock.
So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?
Sep 11 '08
69 3214
So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?
Try to solve the following problem based on your current knowledge.
If you are able to solve it without using the pointers, you don't need
to learn.

/* Puzzle code*/

void X(?????){
???????
}

int main(int cnt, char *aa[]){
int a;
a = 5;
X(??????); //line # 5
printf("\n Value of a is %d",a);
retrun 0;

}

---------------Desired OUTPUT -----------
Value of a is 20

Problem Statement
-----------------------
In the above code, at all the places where you see "?????" you have to
write some C Code.
you have to write the code such that without modifying the variable
"a" in main, value of "a" becomes 20. That is the output of program
when run is as shown in desired output.
Once you finish this, you will realize that there are many cases/
problems which cann;t be solved without using the pointers.
Sep 13 '08 #21
Richard<rg****@ gmail.comwrites :
[...]
Anyway outside of c.l.c pointers are easily taught. They are an address
where some data is. You can de-reference that address to get the data
there. You can advance the pointer to point to different addresses.

Trivial stuff when you do not try to be too clever and blind the poor
nOOB with ridiculous nonsense not applicable to their system at too
early a stage.
Treating pointers simply as machine addresses can easily lead to the
assumption of a single linear address space. That's a common
implementation, but it's not required. For example, as far as C is
concerned, pointers to two independently declared objects have no
defined relationship to each other (other than inequality), and even
computing ``&x < &y'' invokes undefined behavior.

A C pointer is a more abstract concept than you're implying, defined
in such a way that a machine address pointing somewhere in monolithic
linear memory is one way, but not the only way, to implement them.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 13 '08 #22
sh******@gmail. com said:
>
>So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?

Try to solve the following problem based on your current knowledge.
If you are able to solve it without using the pointers, you don't need
to learn.

/* Puzzle code*/

void X(?????){
???????
}

int main(int cnt, char *aa[]){
int a;
a = 5;
X(??????); //line # 5
printf("\n Value of a is %d",a);
retrun 0;

}

---------------Desired OUTPUT -----------
Value of a is 20

Problem Statement
-----------------------
In the above code, at all the places where you see "?????" you have to
write some C Code.
you have to write the code such that without modifying the variable
"a" in main, value of "a" becomes 20. That is the output of program
when run is as shown in desired output.
Once you finish this, you will realize that there are many cases/
problems which cann;t be solved without using the pointers.
void X(int ignore){
int puts(const char *);
void exit(int);
puts("Value of a is 20");
exit(0);
}

int main(int cnt, char *aa[]){
int a;
a = 5;
X(a); //line # 5
printf("\n Value of a is %d",a);
retrun 0;

}

(You might want to try again.)

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 13 '08 #23
sh******@gmail. com writes:
>So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?

Try to solve the following problem based on your current knowledge.
If you are able to solve it without using the pointers, you don't need
to learn.

/* Puzzle code*/

void X(?????){
???????
}

int main(int cnt, char *aa[]){
main's two parameters can legally be given any name you like, but
they're traditionally called argc and argv. Calling them anything
else is obfuscation. And since you don't use them, you can omit them,
declaring main as "int main(void)".
int a;
a = 5;
Ok, but why not use an initializer? "int a = 5;".
X(??????); //line # 5
Um that's not line 5, unless the definition of X is in a separate
source file.
printf("\n Value of a is %d",a);
You have a call to printf. Where's the required #include <stdio.h>?

I don't know where this bizarre habit of putting the "\n" at the
beginning of a line rather than at the end came from. This prints an
unnecessary blank line, and fails to properly terminate the output
line. Some implementations may require a terminating "\n" for valid
output.

printf("Value of a is %d\n", a);
retrun 0;
It's spelled "return". Sure, it's a minor error, but one that you
couldn't have made if you'd bothered to compile your code before
posting it. (Some of my own dumbest mistakes here have been the
result of assuming I could just write code off the top of my head
without bothering to compile it.)
}

---------------Desired OUTPUT -----------
Value of a is 20

Problem Statement
-----------------------
In the above code, at all the places where you see "?????" you have to
write some C Code.
you have to write the code such that without modifying the variable
"a" in main, value of "a" becomes 20. That is the output of program
when run is as shown in desired output.
It's not possible for the value of a to become 20 unless you modify
it. You mean that the code shouldn't *directly* modify a.
Once you finish this, you will realize that there are many cases/
problems which cann;t be solved without using the pointers.
It's probably better just to read about pointers in some good tutorial
or reference work, such as K&R2.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 13 '08 #24
Keith Thompson said:
sh******@gmail. com writes:
>>So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?

Try to solve the following problem based on your current knowledge.
If you are able to solve it without using the pointers, you don't need
to learn.

/* Puzzle code*/

void X(?????){
???????
}

int main(int cnt, char *aa[]){

main's two parameters can legally be given any name you like, but
they're traditionally called argc and argv. Calling them anything
else is obfuscation.
By that argument, these versions are obfuscated too:

int main(int ArgumentCount, char **ArgumentVecto r)
int main(int CommandLineArgC ount, char **CommandLineAr gument)

Longwinded they may be, but I don't agree that they are obfuscated.
And since you don't use them, you can omit them,
declaring main as "int main(void)".
Quite so.

<snip>
I don't know where this bizarre habit of putting the "\n" at the
beginning of a line rather than at the end came from.
Neither do I, but it is well-established, rather like void main.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 13 '08 #25

"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
sh******@gmail. com writes:
>printf("\n Value of a is %d",a);

You have a call to printf. Where's the required #include <stdio.h>?

I don't know where this bizarre habit of putting the "\n" at the
beginning of a line rather than at the end came from. This prints an
unnecessary blank line, and fails to properly terminate the output
line.
At the beginning? It might be at the end of previous line, as in:

for(i=1; i<=10; ++i)
printf("%d ",i);
printf("\nNext line...");

Or serves to separate the program output from whatever preceded it on the
console. Or to guarantee to start at the beginning of a line where the
previous output is not known.

So hardly bizarre.

--
Bartc

Sep 13 '08 #26
* *printf("\n Value of a is %d",a);
>
You have a call to printf. *Where's the required #include <stdio.h>?

I don't know where this bizarre habit of putting the "\n" at the
beginning of a line rather than at the end came from. *This prints an
unnecessary blank line, and fails to properly terminate the output
line. *
Printing a blank line in beginning might be personal preference too,
but i certainly would like to know
what causes failure to properly terminate the output line.

It's spelled "return". *Sure, it's a minor error, but one that you
couldn't have made if you'd bothered to compile your code before
posting it. *(Some of my own dumbest mistakes here have been the
result of assuming I could just write code off the top of my head
without bothering to compile it.)
yea, i didn't compile before posting. My mistake. I will take care of
it in future.
you have to write the code such that without modifying the variable
"a" in main, value of "a" becomes 20. That is the output of program
when run is as shown in desired output.

It's not possible for the value of a to become 20 unless you modify
it. *You mean that the code shouldn't *directly* modify a.
I mentioned without modifying the variable "a" in main. Probably I
should have been more specific here by saying
you have to write the code such that
- you do not modify the variable "a" in function main i.e. you can not
assign any value to variable "a" in function main
- line immediately after call to function X [printf("\n Value of a is
%d",a);] in function main should print as below
Value of a is 20

That will also stop Richard Heathfield's solution without using
pointers from being an answer to this puzzle.
Once you finish this, you will realize that there are many cases/
problems which cann;t be solved without using the pointers.

It's probably better just to read about pointers in some good tutorial
or reference work, such as K&R2.
Did that come after seeing Richard's Solution or you think there still
can be a solution without using call by reference here. I agree call
be reference is actually call be value only in "C" but want to explore
you more on this.

--
vIpIn
Sep 14 '08 #27
Richard Heathfield <rj*@see.sig.in validwrites:
Keith Thompson said:
>sh******@gmail. com writes:
>>>So now I'm wondering: what's the exact position of pointers in C? Is
it really necessary to learn how it works again?

Try to solve the following problem based on your current knowledge.
If you are able to solve it without using the pointers, you don't need
to learn.

/* Puzzle code*/

void X(?????){
???????
}

int main(int cnt, char *aa[]){

main's two parameters can legally be given any name you like, but
they're traditionally called argc and argv. Calling them anything
else is obfuscation.

By that argument, these versions are obfuscated too:

int main(int ArgumentCount, char **ArgumentVecto r)
int main(int CommandLineArgC ount, char **CommandLineAr gument)
Yes.
Longwinded they may be, but I don't agree that they are obfuscated.
You can obfuscate by being overly verbose as well as by being overly
terse.

If I see either
int main(int lI0O0, char **lI0OO)
or
int main(int ArgumentCount, char **ArgumentVecto r)
I have to stop and think about what those two identifiers mean. In
both cases, I'm going to have to realize that what they really *mean*
is argc and argv.

[...]
>I don't know where this bizarre habit of putting the "\n" at the
beginning of a line rather than at the end came from.

Neither do I, but it is well-established, rather like void main.
I think it's a Windows thing, but I don't know why.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 14 '08 #28
"Bartc" <bc@freeuk.comw rites:
"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
>sh******@gmail. com writes:
>>printf("\n Value of a is %d",a);

You have a call to printf. Where's the required #include <stdio.h>?

I don't know where this bizarre habit of putting the "\n" at the
beginning of a line rather than at the end came from. This prints an
unnecessary blank line, and fails to properly terminate the output
line.

At the beginning? It might be at the end of previous line, as in:

for(i=1; i<=10; ++i)
printf("%d ",i);
printf("\nNext line...");

Or serves to separate the program output from whatever preceded it on
the console. Or to guarantee to start at the beginning of a line where
the previous output is not known.

So hardly bizarre.
No, the line
printf("\n Value of a is %d",a);
was the only output call in the program.

Printing "\n" at the beginning of the program's output isn't that bad,
though I see no reason for the superfluous blank line. But omitting
the "\n" at the end of the program's output, unless there's a specific
(and system-specific) reason for it, is wrong.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 14 '08 #29
sh******@gmail. com writes:
[...]
Once you finish this, you will realize that there are many cases/
problems which cann;t be solved without using the pointers.

It's probably better just to read about pointers in some good tutorial
or reference work, such as K&R2.

Did that come after seeing Richard's Solution or you think there still
can be a solution without using call by reference here. I agree call
be reference is actually call be value only in "C" but want to explore
you more on this.
My point was merely that reading a good tutorial is a more effective
way to learn about pointers than reading a single puzzle. I haven't
really thought about other ways to "cheat" on your puzzle.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 14 '08 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

669
26217
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990. http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
12
4214
by: slartybartfast | last post by:
I'm new(ish) to css, comfortable using tables, but trying real hard to move away. Please see http://84.9.125.31/developer/css_test/test5.html NB This issue is with IE & Opera - I've tried IE 6&7 and both have the same result. It works fine in FF. Thanks to Petr Stanicek for the original example. As you can see the table is 800px wide in the middle column and it's vertical position is affected by the amount of text in the left or right...
89
5770
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the right thing to use for every var that holds the number of or size in bytes of things. * size_t should only be used when dealing with library functions.
0
9628
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5367
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3622
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.