• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Simple question on pointers and strings

void getStringComponents(char *tokenString)
   char keyValue1[30];
   char keyValue2[30];
   int numToks;
   numToks= sscanf(tokenString,"%s %s",keyValue1,keyValue2);
   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
  • 3
  • 2
  • 2
  • +1
2 Solutions

> 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,
sambha03Author Commented:
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.
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?

Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

sambha03Author Commented:
They are local variables into which I am trying to read the input string that is passed.
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.

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.

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



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:


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;

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


Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now