I dont know this well.
when we create an array, and we pretend to get its conteud we must do for example printf("%c", *array) or printf("%c", array) ?
its the same thing doing char array[] and char array?
i never understood this.
thanks
I dont know this well.
when we create an array, and we pretend to get its conteud we must do for example printf("%c", *array) or printf("%c", array) ?
its the same thing doing char array[] and char array?
i never understood this.
thanks
#include <stdio.h>
int my_array[] = {1,23,17,4,-5,100};
int *ptr;
if we want to print the whole array we should doCode:int main(void) { ptr = &my_array[0]; /* its the same thing writing &my_array[0] or my_array isnt it? so ptr=my_array */ for (int i = 0; i < 6; i++) { printf("my_array[%d] = %d ",i,my_array[i]); } return 0; }
????Code:printf("my_array = %d", *my_array);
This is an excerpt from my explnation of multidimensional arrays (http://cboard.cprogramming.com/showthread.php?t=64947)
I thought maybe it would help
let's look at at one dimensional array. (let's pretend that an int is one byte, which it isn't)
int a[10];
this reserves 10 bytes of memory. "a" is really a pointer (again, some may argue, but for the sake of simplicity, let's not).
it points to that first byte in memory that is reserved for the array.
all the bytes are guarenteed to be in sequential order when the
array is declared as such. when you ask for a[5], the compiler
finds the address that "a" points to. then, it will move "up" 5
bytes in memory and return the value stored there. a[5] is the
same as *a+5. "a" is referred to as a segment. the five is referred to
as the offset. in short, a[5] and *a+5 says give me the value that is stored in
five bytes past "a".
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
so:
a[5]=*a+5
its the same thing to do char *a than do char a[] isnt it?
this helps much* says a[0]
** says a[0][0]
*** says a[0][0][0]
***a+1 says a[0][0][1]
thank you
char *a != char a[n]Originally Posted by j_spinto
char *a only allocates memory to store a memory address
char a[n] allocates memory to store characters in
in most cases, though, subscripting (using []) and pointer notation are interchangeable, but not necessarily the same.
i forgot to copy the disclaimer when i copied the excerpt...it basically said i was going for logic, not techinicality in my explanation. before imprinting anything into your mind, i suggest reading preludes discussion on pointers vs arrays found here http://faq.cprogramming.com/cgi-bin/...&id=1073086407
Last edited by misplaced; 05-01-2005 at 04:30 AM.
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
yeah i know, i read the entire post and i know that ints never reserve 1 byte memory only
But look, if we do like this:
a=b isnt it?Code:char b[]="this is a string"; char *a="this is a string";
>a=b isnt it?
No, not on any level except for string comparison. a is an array sized and initialized to "this is a string". b is a pointer to the string literal "this is a string". Neither the objects nor the contained strings refer to the same memory locations.
My best code is written with the delete key.
i wasnt talking about having the same memory address but the same conteud
int *b = malloc(2 * sizeof (*b)) is the same as int b[2] this one is correct isnt it? :X
Code:char b[]="this is a string"; // Can be manipulated using string functions in a normal way char *a="this is a string"; // Is stored in read only memory and can not be changed
Thank you all. I now understand quite or less its up to me to know the rest
great forum this
Hugs
No. One is a dynamicly allocated block of memory, which can be used after the function ends, assuming you return it, and the other will be gone (unless declared static) when the function ends. (Assuming the second is in fact declared in a function. The first has to be declared in a function, at least the way you have it.) Also the first one must be freed after you're done with it. The latter will automaticly be destroyed when it goes out of scope. Also, while array names act like pointers in most cases, they aren't.Originally Posted by j_spinto
However, if you're wondering if they're both effectively two integers, yes.
You should read THE FAQ for arrays and pointers.
Quzah.
Hope is the first step on the road to disappointment.
I saw it in the faq lol :P
i am using bloodshed
using the * instead of [] allows me not to define the reserved space for the stringCode:int main(void) { char *array; array="this is a string"; printf("%s\n", array); system("pause"); return 0; }
if i use it do it like this, with the [] instead of *:
it gives me an errorCode:int main(void) { char *array; array="this is a string"; printf("%s\n", array); system("pause"); return 0; }
i have been trying many small apps such as this and i concluded:
- when we are printing a simple array we can never use the * in the printf;
- we cant do *array="string"; but just array="string";
this may help for noobs like me
Um... both of those sample pieces of code are the exact same thing. I'm sure you meant them to be different some how, but they're not.
Furthremore, you seem to be having problems with pointers and arrays. As stated, I suggest again that you go and read the Array / Pointer FAQ that I linked to. You may have "saw" the FAQ. But you most definately haven't read the FAQ. Or, if you have, you definately do not understand the FAQ, because your statements make that clear.
1) You can never assign strings to arrays with the assignment operator. You must copy them an element at a time, or use a function which does the exact same thing (strcpy for example).
2) This is not an array:
It is a pointer to a character, which in this example, points some place randomly. What you're doing here:Code:char *array;
Is assigning the address of a string literal to the pointer. If you change the pointer in any way, this literal is then lost, unless you're keeping track of it with another pointer. You cannot modify string literals in any way. You do not free string literals like you would with dynamic memory allocation + copying something to it.Code:array = "this is a string literal, not an array";
Again, this is not an array in any way shape or form.
3) This of course is wrong:
Because the * denotes dereferncing of the pointer, which is in this case entirely wrong.Code:*array = "this is a string literal";
Quzah.
Hope is the first step on the road to disappointment.