Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-12-2004, 11:41 AM
|
#1
|
Member
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458
Rep:
|
pointer question,
i'm trying to learn pointers, and i have a simple question,
Code:
char str[10] = "hello\0";
char *ptr;
ptr = &str;
printf("%s\n", ptr);
when i can a compile error: incompatiable pointer type.
however when i do this:
Code:
char str[10] = "hello\0";
char *ptr;
ptr = str;
printf("%s\n", ptr);
it compiles and works.
my question is don't you have to reference a pointer before you use it, or do you need to reference an index in an array, like ptr = &str[0].
thanks
|
|
|
08-12-2004, 11:53 AM
|
#2
|
Member
Registered: Jul 2004
Location: Woodland Hills, CA
Distribution: Debian/Mandrake
Posts: 37
Rep:
|
Lets see if this helps...
Say we have this definition:
char ptr = 'A';
Given this, *ptr is not the same as &ptr.
*ptr will evaluate to the character 'A', whereas &ptr would evaluate to the memory address at ptr.
Another thing that needs to be known is these two definitions will be the same...
char *str = "Hello\0";
char str[6] = "Hello\0";
A string in C shouldn't really be thought of as a string, but rather an array of char...
Sorry I don't have time to put more on the subject, but work is nagging at me... Hope it helps though.
|
|
|
08-12-2004, 11:55 AM
|
#3
|
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263
Rep:
|
because of the way arrays work str is a char *const, which points to the first element of the 10 char array. the [] notation works like str[z]=*(str+z) so ptr=&str[0] works out as the address of the first character of str ie str, this is shown below
ptr=&str[0]
ptr=&*(str+0)
ptr=&*(str)
ptr=str
if you remember that arrays are just pointers that point to the start of the array and you remember that x[z]=*(x+z) then you should have no problems.
<edit>
by Ankheg
Say we have this definition:
char ptr = 'A';
Given this, *ptr is not the same as &ptr.
*ptr will evaluate to the character 'A',
no it wont, *ptr makes no sense as ptr isnt a pointer
whereas &ptr would evaluate to the memory address at ptr.
&ptr would evaluate to the memory address of ptr
Another thing that needs to be known is these two definitions will be the same...
char *str = "Hello\0";
char str[6] = "Hello\0";
NO, these definitions must be thought of as very different, the first creates a 4 byte pointer on the stack and puts in it the address of the string "Hello\0" which is normally stored in a read only area. the second creates a 6 byte array on the stack(which is not big enough to hold your string btw) and copies into it the string "Hello\0". the two statements are very very different
</edit>
Last edited by kev82; 08-12-2004 at 12:06 PM.
|
|
|
08-12-2004, 12:01 PM
|
#4
|
Member
Registered: Jul 2004
Posts: 96
Rep:
|
str is an array of chars. In C, an array of type is just a type *; str[n], jjust translates to: *(str+sizeof(char)*n) ..
So you could have written just
char * str = "hello" ;
and defined like that you can use str[3] just as well.
BTW, " " quotes denote a null terminated string so you do not need to put a \0 on the end (it is assumed) of the string (unless, although I doubt it, you want a double-null-terminated string). Therefore "hello" is the same as 'hello\0'
Hope that helps,
Maksim Sipos
|
|
|
08-12-2004, 02:46 PM
|
#5
|
Member
Registered: Jul 2003
Distribution: Slackware 9.1
Posts: 195
Rep:
|
I think you can put it even more simply by saying that you can't use the & operator to get the address of a constant. The array base 'str' is a constant, although you can refer to it using pointer notation.
|
|
|
08-12-2004, 09:07 PM
|
#6
|
Member
Registered: Dec 2003
Location: New Zealand
Distribution: Slackware 10.2
Posts: 36
Rep:
|
Hi, in your program both,
are of type pointer to character, where 0 <= x < number_of_elements. On the other hand,
is of type pointer to array of character. If you want a variable of type pointer to array of character you can do something like this:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef char (*arr_ptr)[];
int main(int argc, char **argv)
{
char string[] = "Hello";
arr_ptr ptr;
ptr = &string;
printf("%s\n", *ptr);
exit(EXIT_SUCCESS);
}
gcc -Wall -pedantic-errors -o ptr_test ptr_test.c
Here's a link to the best online text regarding C I've ever found.
The C book
|
|
|
08-13-2004, 02:21 AM
|
#7
|
Member
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185
Rep:
|
To be very simple is that.
case 1: char *ptr; // "ptr" is pointer but no memory is allocated to it.
case 2: char ptr[10]; // "ptr" is a pointer and 10 bytes are allocated to it.
When you allocate memory to first case.
char *ptr;
ptr = new char[10];
Now you can say that is equal to the second case.
Just consider in case two we say "ptr" is a pointer but there is no " * " in its defination. Just suppose that ,
[ ] == *
you can not use both at once to declare a one dimentional pointer.
if you define,
char *ptr[10];
this means its a double pointer, Coz it has both [ ] and a *.
_________________________________________________________
Now comming to your question, why ptr = &str not work but ptr = str works.
The reason is that in your case,
ptr is a pointer (contains an address).
str is a pointer (contains an address).
When you say ptr = str. Its ok.
but when you say ptr = &str. It means that ptr will hold the address of str, and str itself contain an address, and you are not copying the content of str to ptr but the address of str to ptr.
Also keep in mind when you define
char str[10];
the variable str contains the address of the first location of the allocated memory.
As i previously mentioned that * == [ ], and as you know that to see the value of a location we use *, like ,
int *ptr;
output of ptr will give you an address.
outout of *ptr will give you the value stored at that address.
so when you hav char str[10];
and you want to see the value at first location you will write.
print str[0];
as [ ] == * so you can also write
print *str;
As i said that str contains the address of first locatin so both will print the same. aslo you can check it by,
print str[1] ===== print *(str + 1)
print str[4] ===== print *(str + 4)
so str is basically a pointer that contains the address of the first location of array and you want your pointer ptr to contain that address, you just write
ptr = str
Hope you will understand
|
|
|
All times are GMT -5. The time now is 11:08 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|