473,661 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer question (pretty basic)...

I never liked pointers really much, so I decided to stay away from
them for a while. I know they're useful, so now I decided to actually
learn how the work, use them, etc.

Here's my question...

char *a = "some text";
char *b = "some other text";

Why will this work? Needn't I initialize the pointers first? (malloc
in C, new in C++, whatever)

And second question. Let's say I got a function that returns char *
("odd" if the parameter is odd, and "even" if the parameter is even)

char *s1 = CHAR_PTR_FUNCTI ON(1);
char *s2 = CHAR_PTR_FUNCTI ON(2);

The return value (returned like 'return "odd"' or 'return "even"'),
where is it stored? It has to be stored somewhere cuz otherwise *s1
would have the same value as *s2...

I'm getting confused here...

Any help?
Nov 14 '05 #1
3 1772
cm****@yahoo.co m (Chris Mantoulidis) wrote in
news:a8******** *************** ***@posting.goo gle.com:
I never liked pointers really much, so I decided to stay away from
them for a while. I know they're useful, so now I decided to actually
learn how the work, use them, etc.
They are extremely powerful and are integral to the C language, I suggest
you become proficient in using them.
Here's my question...

char *a = "some text";
char *b = "some other text";

Why will this work? Needn't I initialize the pointers first?
You just did. You are allocating two pointers and them initializing them
to point to two strings stored somewhere else. Just don't try to write to
these pointers. In this case, you'd have been better to use const char
*pThing instead of char *pThing.
And second question. Let's say I got a function that returns char *
("odd" if the parameter is odd, and "even" if the parameter is even)

char *s1 = CHAR_PTR_FUNCTI ON(1);
char *s2 = CHAR_PTR_FUNCTI ON(2);

The return value (returned like 'return "odd"' or 'return "even"'),
where is it stored? It has to be stored somewhere cuz otherwise *s1
would have the same value as *s2...


The strings "odd" and "even" are stored somewhere that you don't need to
care about. Returning a point to a string of this type is save and you can
safely set your s1 and s2 pointer to point to them via the function
return. Pointer s1 and s2 may point to the same string in memory if both
numbers are even or odd, so what? As long as you don't try to write to the
strings pointed to by s1 or s2 you're fine.

--
- Mark ->
--
Nov 14 '05 #2
In <a8************ **************@ posting.google. com> cm****@yahoo.co m (Chris Mantoulidis) writes:
I never liked pointers really much, so I decided to stay away from
them for a while.
Without pointers, C is practically useless. Even the canonical "hello
world" program is using a pointer.
Here's my question...

char *a = "some text";
char *b = "some other text";

Why will this work? Needn't I initialize the pointers first? (malloc
in C, new in C++, whatever)

And second question. Let's say I got a function that returns char *
("odd" if the parameter is odd, and "even" if the parameter is even)

char *s1 = CHAR_PTR_FUNCTI ON(1);
char *s2 = CHAR_PTR_FUNCTI ON(2);

The return value (returned like 'return "odd"' or 'return "even"'),
where is it stored? It has to be stored somewhere cuz otherwise *s1
would have the same value as *s2...

I'm getting confused here...


Open your C book and read about string literals. You'll find the answer
to both your questions.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #3
"Mark A. Odell" <no****@embedde dfw.com> wrote in message news:<Xn******* *************** **********@130. 133.1.4>...
cm****@yahoo.co m (Chris Mantoulidis) wrote in
news:a8******** *************** ***@posting.goo gle.com:
I never liked pointers really much, so I decided to stay away from
them for a while. I know they're useful, so now I decided to actually
learn how the work, use them, etc.


They are extremely powerful and are integral to the C language, I suggest
you become proficient in using them.
Here's my question...

char *a = "some text";
char *b = "some other text";

Why will this work? Needn't I initialize the pointers first?


You just did. You are allocating two pointers and them initializing them
to point to two strings stored somewhere else. Just don't try to write to
these pointers. In this case, you'd have been better to use const char
*pThing instead of char *pThing.

And second question. Let's say I got a function that returns char *
("odd" if the parameter is odd, and "even" if the parameter is even)

char *s1 = CHAR_PTR_FUNCTI ON(1);
char *s2 = CHAR_PTR_FUNCTI ON(2);

The return value (returned like 'return "odd"' or 'return "even"'),
where is it stored? It has to be stored somewhere cuz otherwise *s1
would have the same value as *s2...


The strings "odd" and "even" are stored somewhere that you don't need to
care about. Returning a point to a string of this type is save and you can
safely set your s1 and s2 pointer to point to them via the function
return. Pointer s1 and s2 may point to the same string in memory if both
numbers are even or odd, so what? As long as you don't try to write to the
strings pointed to by s1 or s2 you're fine.


Thanks, it's clear at last =P
Nov 14 '05 #4

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

Similar topics

4
7531
by: gorda | last post by:
Hello, I have the following code, with a basic class having a constructor and destructor. in the main section, I create a static pointer to the class. My question is regarding the destructor. If I dont explicitly delete the pointer, the destructor is called. However, I expected that even with out explicitly deleting it, the destructor would be called when the program ends. #include <iostream>
3
3666
by: Charlie | last post by:
My Google Groups search on this one showed the endless debates and I did my best to look through them to see if my question was answered and didn't find it. So, here it goes.... From the GG search, I have pretty much gathered and understand why using sizeof(pointer to char) returns 4 in my program. Is there a way to get it to return the actual amount of characters? the basic idea is that I am initializing a char and passing it into a...
42
5910
by: junky_fellow | last post by:
Consider an implementation that doesn't use all bits 0 to represent a NULL pointer. Let the NULL pointer is represented by 0x12345678. On such an implementation, if the value of NULL pointer is printed will it be all 0's or 0x12345678 int main(void) { char *ptr; ptr = 0;
18
2116
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for last_values as well as assign the value of a pointer to the assigned space. Which I think I'm doing by using:
51
4434
by: Kuku | last post by:
What is the difference between a reference and a pointer?
33
5049
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
92
3295
by: =?Utf-8?B?bW9iaWxlbW9iaWxl?= | last post by:
I'm trying to load this structure for a call to DeviceIoControl: typedef struct _NDISUIO_QUERY_OID { NDIS_OID Oid; PTCHAR ptcDeviceName; UCHAR Data; } NDISUIO_QUERY_OID, *PNDISUIO_QUERY_OID; I created the equivalent in VB:
5
1394
by: BillGill | last post by:
Ok, I assume this has been asked many times, but I can't seem to come up with a good Google search to find it. I am trying to learn C++. Specifically I have Microsoft Visual C++ 2005 Express Edition. I am trying to learn it from Ivor Horton's Beginning Visual C++ 2005. My problem comes when we get to pointers. I just cannot seem to wrap my mind around the complexities involved. Is there some place, either a book or a website that...
6
1982
by: Immortal_Nephi | last post by:
This Run() function is function pointer. It contains three functions. How can you put const in Run() function? It should guard against modifying Run()'s function pointer array. Do you know what do const before void mean? For example -- const void foo() {}. class Obj { public: Obj();
0
8343
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,...
0
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
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
8633
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
6185
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
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.