1

I'm confused as to why I can create a string by using just char*. Isn't a char* just a pointer to a single char? What makes me suddenly allowed to treat this thing as a string? Can I do this with other objects? What about:

int* arr;

Do I now have an array of ints? Is it dynamically sized? Can I just start calling

arr[0] and arr[1]

and expect it to work?

I've been googling for quite awhile now, and can't find any clear answer to this question. This is one part of C I've always just accepted as the way it works...no more.

Roguebantha
  • 814
  • 4
  • 19
  • You need to read a good book (rather than google). To differentiate between char* and char arrays read [this answer](http://stackoverflow.com/questions/15177420/what-does-sizeofarray-return/15177499#15177499) - read second part of the answer – Grijesh Chauhan Mar 27 '16 at 05:03
  • "Isn't a `char*` just a pointer to a single char?" No not necessarily, it's a pointer to **a** char. It doesn't say anything about whether or not there are other chars (or what is interpreted as char) nearby. – Jeff Mercado Mar 27 '16 at 05:22
  • See http://www.c-faq.com/aryptr/index.html. In particular: http://www.c-faq.com/aryptr/aryptrequiv.html – jamesdlin Mar 27 '16 at 06:40

2 Answers2

2

Pointers point to things. int *arr; currently does not point anywhere. You cannot treat it as pointing to anything (array or otherwise).

You can point it at something. If you point it at a single int, you can treat it as pointing to a single int. If you point it at an element of an array of ints, you can treat it as pointing to an element of an array.

char behaves the same as int (or any other object type) in this respect. You can only treat a char * as pointing to a string if you first point that char * to a string.

M.M
  • 138,810
  • 21
  • 208
  • 365
1

Isn't a char* just a pointer to a single char?

It is a pointer to a memory location that should hold a char. It can be a single allocated char, or an array of several char's.

What makes me suddenly allowed to treat this thing as a string?

When the char* pointer points to a memory location where several chars[] are allocated one after another (an array), then you can access them with an array index [idx]. But you have to either:

A. Use a statically allocated array in code:

char myArray[5];

B. or Dynamic allocation (malloc() in C language):

int chars_in_my_array = 10;
char* myArray = (char*)malloc( sizeof(char) * chars_in_my_array );

Can I do this with other objects? What about: int* arr; Do I now have an array of ints?

no you don't:

int* p;         //'p' is a *pointer* to an int 
int arr[10];    //'arr' is an array of 10 integers in memory.

You can set 'p' to point to the first element in 'arr' by doing this:

p = &arr[0];    //'p' points to the address of the first item in arr[]

Is it dynamically sized?

No. That's something you'd see in a higher-level construct like 'std::string' in C++, or 'String' in Java. In C, memory is managed manually (or you use existing library functions to do this for you. see the standard header file 'string.h')

Can I just start calling arr[0] and arr[1] and expect it to work?

Not unless you do this:

int arr[5];  
arr[0] = 100; arr[1] = 200;

...Because you statically allocate an array of five integers. Then you can access them with an array subscript.

However you can NOT do this:

int* p;
p[0] = 100; p[1] = 200; // NO!!

Because 'p' is just a variable that contains a memory address to an integer. It doesn't actually allocate any memory unless you explicitly allocate memory for it. You can do this though:

int* p;
int arr[10];
p = &arr[0];
p[0] = 100; p[1] = 200; // This is OK

This works because 'p' now points to memory that has been (statically) allocated for an array. So we can access the array through the pointer 'p'.

You can also dynamically allocate the array:

int* p;
p = (int*)malloc( sizeof(int) * 10 );
if( p != NULL ) {
    p[0] = 100; 
    p[1] = 200;
}

Here we allocate memory for 10 integers and 'p' points to the first item. Now we can use 'p' as an array. But only for 10 items maximum!

I've been googling for quite awhile now, and can't find any clear answer to this question. This is one part of C I've always just accepted as the way it works...no more.

Really? Try googling 'arrays in c', 'memory allocation in C', 'static vs dynamic allocation C', and functions such as malloc() and free(). There's a plethora of information available, but hope this helps for starters :)

jrsmolley
  • 399
  • 1
  • 7