-
#include<stdio.h>
-
int main(void)
-
{
-
int *p="Hello_i_am_abhijeet"; //Whats this actually doing???
-
char q[]="Hello_i_am_abhijeet"; //This is OK
-
-
printf("%c %c\n",p[2],q[2]); //Here p[2] shows "a" that means
-
//due to p[2], pointer moves 8 bytes
-
return 0;
-
}
-
Terminal Output: codefire@codefire-desktop:~$ gcc test.c
test.c: In function ‘main’:
test.c:4: warning: initialization from incompatible pointer type
codefire@codefire-desktop:~$ ./a.out
a l
codefire@codefire-desktop:~$
I want to know as to what actually happens in line 4 of program.
I know line 4 may look weird.
But, i was curious to know. And hows is the output coming, i mean whats theory behind that.
Also, it would be great if you pls help me understanding the difference between "char *a" and "char a[]". I tried searching on net but couldnt get satisfied with what i understood.
Your reply will be of great help to me.
9 1941
Yes, it's mighty weird. Like it doesn't compile. "Hello_i_am_abhijeet" is a literal string which is a char array. The address of the H is a char*. You cannot assign a char* to an int*.
If your compiler accepts this, then get a proper compiler.
Next: - char* c = "Hello_i_am_abhijeet";
-
char d[] = "Hello_i_am_abhijeet";
In this example c is a pointer to a char. That char is the H of Hello. The literal itself is a char array but is constant so you cannot chnage the letters in the array.
On the other hand, d is an array of char but the number of elements is not provided. In this case the compiler counts the letters in "Hello_i_am_abhijeet" and adds 1 for a null terminator. Then it creates the d array for that number oc characters and copies "Hello_i_am_abhijeet" to the d array. This time d is not constant so you can change the letters in the array. This only works when d is created. Later on if you try: -
d = "New letters"; //ERROR
-
d[] = "New letters"; //ERROR
-
you get errors. To change d you would copy in new values: - strcpy(d, "New letters");
strcpy does not check that the new letters will fit in the d array. It's up to you to manage this.
@weaknessforcats
I used GCC as it can be understood through the terminal output i posted above..
But, still i didnt get the answer of what I actually asked.
Can anybody answer? I am really curious about this peculiarity.
@weaknessforcats
I used GCC as it can be understood through the terminal output i posted above..
But, still i didnt get the answer of what I actually asked.
Can anybody answer? I am really curious about this peculiarity.
donbock 2,426
Recognized Expert Top Contributor @shadyabhi
Nothing happens in line 4 -- it doesn't compile.
donbock 2,426
Recognized Expert Top Contributor @shadyabhi - char *a = "This is a string";
-
char b[] = "So is this";
In the first case, the string (characters + null termination) is placed in an unnamed location and the address of that location is stored in variable a. sizeof(a) = the size of a pointer (typically 4 bytes).
In the second case, the string (characters + null termination) is placed in a buffer starting at location b. sizeof(b) = 11.
thanx for reply ...
But, i tell you dude, that program(the exact code which i posted) compiles gcc 4.2.4...
Which compiler are you using??
>But, i was curious to know. And hows is the output coming, i mean whats theory behind that.
Assuming that program executes on little-endian machine, printf("%c", p[2]) prints ascii value of the lowest byte of p[2], and the address of this byte is the same as address of p[2]. As sizeof(int) is most likely 4, it's the byte with an offset of 8 from the lowest byte of p[0], and lowest byte of p[0] is q[0].
Then your gcc is using non-standard syntax rules.
You might try the -pedantic switch to disable non-standard features.
However, if you have the address of a char stored in an int* and you increment the int*, the compiler will advance sizeof(int) rather than sizeof(char) to totally screw up your memory.
So, if this is compiling, it is out of gross negligence to the pointer arithmetic rules.
donbock 2,426
Recognized Expert Top Contributor @shadyabhi @shadyabhi
gcc 4.2.4 reports warning: initialization from incompatible pointer type. It isn't any happier about line 4 than I am. True, I overstated matters when I said "it doesn't compile". You get a compiler warning, not a compiler error; but that isn't good news.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Alexander Zatvornitskiy |
last post by:
Hello All!
I'am novice in python, and I find one very bad thing (from my point of view) in
language. There is no keyword or syntax to declare variable, like 'var' in
Pascal, or special syntax in...
|
by: Anon Email |
last post by:
Hey people,
This looks really weird. I can't make sense of it. Is this a mistake?
Can anyone help?
IStack const & GetStack () const;
Cheers,
|
by: Sönke Tesch |
last post by:
Hi everybody,
I have a problem with the following piece of code:
141: /* A data block to manage a single log target: */
142: typedef struct {
143: apr_reslist_t *dbs; /* connection pool */...
|
by: Nils Emil P. Larsen |
last post by:
Hello
I have read about a C shared library which I want to use in my C
program. (It's a library to encode/decode packets from/to a serial bus
running with the SNAP-protocol).
Unfortunatly...
|
by: Holger (David) Wagner |
last post by:
Hi all,
we're currently developing an application in the .NET environment that
needs to access a DLL implemented in Fortran. There is one procedure
with about 17 parameters, most of them arrays....
| |
by: Wild Wind |
last post by:
Hello,
I have an event in my managed class which I declare as:
__event void MyEvent(System::Byte (*fir) __gc);
However, I find that when I build the class, I get very
strange errors like
...
|
by: Wild Wind |
last post by:
Hello,
I posted this a few days back, but got no response, so I
am reposting it.
I have an event in my managed class which I declare as:
__event void MyEvent(System::Byte (*fir) __gc);
...
|
by: Nak |
last post by:
Hi there,
It's probably me being weird more than the function but I'm having
problems with it doing as it should.
I have a C++ application with 2 resources of custom types,
RT_INIFILE @...
|
by: vlsidesign |
last post by:
Here is my a portion of my program:
#include <stdio.h>
main()
{
int fahr, celsius;
int lower, upper, step;
int fc_conv(int fahr);
... snip ...
}
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |