473,473 Members | 4,257 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A weird declaration: int *a="any_string"

18 New Member
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main(void)
  3. {
  4.     int *p="Hello_i_am_abhijeet";    //Whats this actually doing???
  5.     char q[]="Hello_i_am_abhijeet";    //This is OK
  6.  
  7.     printf("%c %c\n",p[2],q[2]);          //Here p[2] shows "a" that means 
  8.                               //due to p[2], pointer moves 8 bytes 
  9.     return 0;
  10.  
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.
Mar 14 '09 #1
9 1941
weaknessforcats
9,208 Recognized Expert Moderator Expert
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:
Expand|Select|Wrap|Line Numbers
  1. char* c = "Hello_i_am_abhijeet";
  2. 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:

Expand|Select|Wrap|Line Numbers
  1. d  = "New letters";  //ERROR
  2. d[] = "New letters";  //ERROR
  3.  
you get errors. To change d you would copy in new values:
Expand|Select|Wrap|Line Numbers
  1. 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.
Mar 14 '09 #2
shadyabhi
18 New Member
@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.
Mar 16 '09 #3
shadyabhi
18 New Member
@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.
Mar 16 '09 #4
donbock
2,426 Recognized Expert Top Contributor
@shadyabhi
Nothing happens in line 4 -- it doesn't compile.
Mar 16 '09 #5
donbock
2,426 Recognized Expert Top Contributor
@shadyabhi
Expand|Select|Wrap|Line Numbers
  1. char *a = "This is a string";
  2. 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.
Mar 16 '09 #6
shadyabhi
18 New Member
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??
Mar 16 '09 #7
newb16
687 Contributor
>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].
Mar 16 '09 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Mar 16 '09 #9
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.
Mar 16 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

83
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...
11
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,
5
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 */...
2
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...
3
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....
0
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 ...
1
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); ...
14
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 @...
2
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 ... }
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
tracyyun
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...
1
isladogs
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.