Solved

Simple question on pointers and strings

Posted on 2003-11-06
9
202 Views
Last Modified: 2010-04-15
CASEA:
void getStringComponents(char *tokenString)
{
   char keyValue1[30];
   char keyValue2[30];
   int numToks;
   numToks= sscanf(tokenString,"%s %s",keyValue1,keyValue2);
}
CASEB:
   numToks= sscanf(tokenString,"%s %s",&keyValue1,&keyValue2);

Both sscanf seem to work fine. Is CASEB incorrect for some reason? I thought &keyValue1 will refer to same address as keyValue1 being &keyValue1[0]. Pls explain
0
Comment
Question by:sambha03
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9697112
sambha03:

> Both sscanf seem to work fine. Is CASEB incorrect for some reason? I thought
> &keyValue1 will refer to same address as keyValue1 being &keyValue1[0]. Pls explain

Almost...  "keyValue1" by itself is the same as &(keyValue1[0]).  That is, it points to the first element in the array.

&keyValue1 takes the address of the pointer that points to the first element in the array.

Hope that helps,
Dex*
0
 

Author Comment

by:sambha03
ID: 9697193
So isnt that the same? Cos the pointer  that points to the first element of the array is keyvalue1 itself. It seems to wrk fine.
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9697386
No, they aren't the same.

Say you do this:
     char SomeString[50];

That will allocate space for 50 chars.  Let's just say that these 50 chars occupy addresses 10 through 59.

Now, you have the following values:
     SomeString              = 10 (the location of the first element)
     SomeString[0]          = The VALUE stored at address 10
     &(SomeString[0])     = The address of the VALUE stored at address 10 (which is 10, obviously)
     &SomeString            = The address of SomeString

In your CASEB, how are keyValue1 and keyValue2 defined?

Dex*
0
 

Author Comment

by:sambha03
ID: 9697552
They are local variables into which I am trying to read the input string that is passed.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Accepted Solution

by:
efn earned 50 total points
ID: 9697556
keyValue1 is an array.  When you pass it to a function, it automatically gets converted to a pointer to the first element of the array.

&keyValue1 is a pointer to the array.  It's already a pointer, so it doesn't get converted when you pass it to a function.  The array is located at the same place as its first member, so the pointer to the array and the pointer to the first element have the same value, which is why they are interchangeable to sscanf.  But they don't have the same type to the compiler.  You can check this with this code:

      char ar[30];
      char* p1 = ar;
      char* p2 = &ar;       //Error!  Pointer to array of char is not the same as pointer to char.
      ar (*p3)[30] = ar;  //Error!
      char (*p4)[30] = &ar;    // OK--p4 is a pointer to an array.

Also note that this distinction is specified for ISO C, but an old enough compilermay be more lenient.

So while both forms will work with sscanf, CASEA is technically more correct.

--efn
0
 
LVL 19

Assisted Solution

by:Dexstar
Dexstar earned 75 total points
ID: 9697590
Yeah, even if CASEB works, you should do it the way CASEA is done because it is more correct.  CASEB may seem to work, but it could cause weird random results at some point.

Dex*
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9698938
One more point to add

when u declare an array like char a[10] OR char[] = "HelloWorld";

a is actually a const pointer. Thus the compiler won't allow to have it's addrss

However if u have an actual char *, i.e. char *p = new char[10] ;

Then p is NOT a const pointer so u can take the address of p

HTH

Amit



0
 
LVL 15

Expert Comment

by:efn
ID: 9699233
An array is not a pointer.  Admittedly, the design of C makes this confusing, because in many cases it lets you use an array as a pointer or a pointer as an array, or it automatically converts an array reference to a pointer to the array's first element.  But still they are different types of objects.  A pointer is an address, while an array is a sequence of objects of the same type.  Pointers are typically always the same size, no matter what they address.  The size of an array varies depending on what type of objects it contains and how many there are.

For a more detailed explanation, see this tutorial:

http://pw1.netcom.com/~tjensen/ptr/pointers.htm

The compiler has no objection to taking the address of a const pointer that is an lvalue, that is, one that has an address.  For example, the following code compiles:

      const char c = 'a';
      const char* const p1 = &c;
      const char* const * const p2 = &p1;

--efn
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9699277
I do not agree with u efn

The compiler does not do anything special to let u treat an array as a pointer

Actually spaking, as I posted earlier, an array is actually handled as a const pointer

And the const modification, C "DOES NOT" handle const properly

Also try this

char *p = new char[10] ;
strcpy ( p, "HelloWorld" ) ;

char * const  ptr = p ; // You WON'T be able to change what ptr points to once u initialize

char * ptr1 = p ;  // You would  be able to change what ptr points to

char arr[10] = "HelloWorld" ;

Although u can access arr as a pointer but u cannot make arr point to some other memory location

i.e. arr is a const pointer



0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now