Solved

Character array..........

Posted on 2003-10-30
6
214 Views
Last Modified: 2010-04-15
Hi,
 
  When i compiled the following program, I got "9" as output. I couldnt able to understand the line "p=(buf+1)[5]".  What this line does?

 main()
{
  char *p;
  char buf[10] ={ 1,2,3,4,5,6,9,8};
  p = (buf+1)[5];
  printf("%d" , p);
}

Thanx.
0
Comment
Question by:hemanexp
6 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9649408
buf gives you the base address of array. i.e. address of first elemnet, in your case address of 1.

Now (buf + 1) would give you address of 2nd location i.e. 2

And then (buf+1)[5] means 6th location from (buf+1)

And u are assigning the value at (buf+1)[5] to p

Thus output
0
 
LVL 5

Accepted Solution

by:
dennis_george earned 20 total points
ID: 9649420
hi,

 buf  --> address(pointer to) of the first value of the array

 so (buf  + 1 ) --> address of the second member of array (since array data is stored in continuous memory)

 an array (buf[x])  can also be represented by

  buf[x] --> *(buf + x) --> *(x + buf) --> x[buf] ;

 so

 (buf + 1)[5] -- > *( (buf +1) + 5) --> *(buf + 6) --> buf[6] --> 6[buf]

 Hope you got this

Dennis
 
 
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9649439
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 6

Expert Comment

by:Ajar
ID: 9649849
char *p;
char buf[10] ={ 1,2,3,4,5,6,9,8};

//buf[0]    buf[1]      buf[2]      buf[3]     buf[4]   buf[5]  buf[6]    buf[7]  .....
//SAME AS
//buf+0    buf+1      buf+2        buf+3     buf+4    buf+5  buf+6    buf+7  .....
//1          2             3              4            5           6         9         8


//so       (buf+1)[5]  =                                                  VVV
//            pos0        pos1        pos2       pos3      pos4    pos5    pos6

//which is  9
 
0
 

Expert Comment

by:fatimao
ID: 9663332
hi!

The name of ur array i.e. currently buf, always point to the start of array (logical address) and as u might know that arrays are consecutie memory location.
So if we say that buf is pointing to memory area 1245 (buf -> 1245 ) then there are 10 locations reserved for it (as the space specified was 10) from location 1245 to 1254.

buf[0] -> 1  (memory location 1245)
buf[1] -> 2  (memory location 1246)
   .
   .
   .
buf[9] -> 8  (memory location 1254)


When u write buf[5] it is equivalent that u are pointing five positions ahead from the start of array i.e 1249.
More over (buf+1) is equivalent to buf[1] so wat (buf+1)[5] eventually means fifth location starting from the first location (1246)of array buf. i.e 6th location (1250). thus giving u the value 9.

hope it helps.
regards
fa
0
 
LVL 5

Expert Comment

by:dennis_george
ID: 9856308
If you got your problem solved then please close this question.....

Dennis
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
logging Access violation 6 19
libcurl - taking long time on curl_easy_perform 11 405
C++ vs C compilers 13 159
Synchronization using condition variables to avoid locking in C 8 78
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question