Unclear pointer and array


 
Thread Tools Search this Thread
Top Forums Programming Unclear pointer and array
# 15  
Old 01-18-2014
Thanks Akshay!
Seems I understand sprinf() to return a string, but could you please explain this line?
Code:
sprintf(dst, "%.*s", count, src + start);

suppose the input string is "A test string" from start = 3, end = 9.
Or, more specifically the part "%.*s", which I assume is for the formats. How does it work? Thanks again.

Last edited by yifangt; 01-18-2014 at 11:21 AM..
# 16  
Old 01-18-2014
Could this help you ?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main(){

int start = 3;
int stop  = 9;
const char src[] = "A test string";

// print 6 char from src+start 
printf("%.*s\n",stop-start, src + start);

// &src[3] == src+3
printf("%s %s\n", &src[3], src+3);

// what is src+start ?
printf("src + %d = %s\n",start,src+start);

// print nchar from src+start
int nchar = 9;
printf("%.*s\n",nchar, src + start);


}

Code:
$ gcc test2.c
$ ./a.out 
est st
est string est string
src + 3 = est string
est strin

---edit----

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main(){

int start = 3;
int stop  = 9;
const char src[] = "A test string";

// I confused from  variable "count" that whether you like to print "char-start to char-stop" '3 - 9' or n char from start ?
unsigned int count = stop - start;

// print 6 char from src+start 
printf("char %d-%d = %d ~ %.*s\n",start,stop,count,count, src + start);

// n char specified in stop from starting
printf("Total char %d from start ~ %.*s\n",stop,stop,src + start);


}

Code:
$ ./a.out 
char 3-9 = 6 ~ est st
Total char 9 from start ~ est strin


Last edited by Akshay Hegde; 01-18-2014 at 01:24 PM..
This User Gave Thanks to Akshay Hegde For This Post:
# 17  
Old 01-18-2014
Quote:
Originally Posted by yifangt
Thanks Akshay!
Seems I understand sprinf() to return a string, but could you please explain this line?
Code:
sprintf(dst, "%.*s", count, src + start);

suppose the input string is "A test string" from start = 3, end = 9.
Or, more specifically the part "%.*s", which I assume is for the formats. How does it work? Thanks again.
You need to look at the man page for the printf() function. You'll see that:
Code:
sprintf(dst, "%.*s", count, src + start);

is shorthand for:
Code:
char fmt[10];
sprintf(fmt, "%%.%ds", count);
sprintf(dst, fmt, src + start);

without the need for the space for the intermediate format string.
This User Gave Thanks to Don Cragun For This Post:
# 18  
Old 01-18-2014
Hi Don!
Your reply confused me more. (It seems a typo for man page for sprintf() instead of printf() function. Right?). Yes, I did look at the man page first. The closest part from the manpage of sprintf is:
Code:
int sprintf(char *str, const char *format, ...);

sprintf(),  snprintf(),  vsprintf()  and vsnprintf() write to the character string str.
 One  can  also specify explicitly which argument is taken, at each place where an argument is required, by writing "%m$" instead of '%' and "*m$" instead of '*', 
where the decimal integer m denotes the position in the argument list of the desired argument, indexed starting from 1.  Thus,
           printf("%*d", width, num);    and     printf("%2$*1$d", width, num);  are equivalent.

Still not clear to me.
For Akshay, I am fine with the pointer moving like src+start. The difficult part is the "%.*s", which is my first time to see it, and I was thinking:
Code:
sprintf(dst, "%.*s", count, src + start);

is equal to:
Code:
sprintf(dst, "%s %d", count, src + start);

Is that right? But Don's line
Code:
sprintf(fmt, "%%.%ds", count);

confused me even more.
The format specification pointers and address with printf() is one of the most difficult part for me. Whenever I saw warning or error msgs like:
Code:
warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘size_t *’ [-Wformat] etc.

I panic and bang my head on the table.

Last edited by yifangt; 01-18-2014 at 03:24 PM..
# 19  
Old 01-18-2014
Quote:
Originally Posted by yifangt
Hi Don!
.....
Code:
sprintf(dst, "%.*s", count, src + start);

is equal to:
Code:
sprintf(dst, "%s %d", count, src + start);

Is that
....
The format specification pointers and address with printf() is one of the most difficult part for me. Whenever I saw warning or error msgs like:
Code:
warning: format ‘%c' expects type ‘int', but argument 2 has type ‘char *' [-Wformat]
warning: format ‘%c' expects type ‘int', but argument 2 has type ‘size_t  *' [-Wformat] etc.

I panic and bang my head on the table.
swap argument like this sprintf(dst, "%s %d", src + start,count);

Let me take one example printf("%*d", 5, 10) will result in " 10" being printed, with a total width of 5 characters, and
printf("%.*s", 3, "akshay") will result in "aks" being printed.

Code:
#include <stdio.h>
main(){
             printf("%*d\n", 5, 10); 
             printf("%.*d\n", 3, 10); 
             printf("%.*s\n", 3, "akshay");
      }

Code:
$ ./a.out 
   10
010
aks

Code:
#include <stdio.h>
main(){
           // Equal
           printf("%.*s\n", 3, "akshay");
           printf("%.3s\n", "akshay" );
      }

Code:
$ ./a.out 
aks
aks

Don's approach is not confusing me

Code:
#include <stdio.h>
main(){
          int count = 3;
          char fmt[10];
          char src[6] = "akshay";
          char dst[6];

          // Don's approach : following statements creates format that is
          // fmt will be "%.3s"
          sprintf(fmt, "%%.%ds", count);

          printf("Format is : %s\n", fmt);

          // following statement is equal to sprintf(dst,"%.3s",src)
          sprintf(dst, fmt, src);
          printf("output is : %s\n", dst);
      }

Code:
$ ./a.out 
Format is : %.3s
output is : aks


Last edited by Akshay Hegde; 01-18-2014 at 03:58 PM.. Reason: more detail
This User Gave Thanks to Akshay Hegde For This Post:
# 20  
Old 01-18-2014
Now I got it!
Code:
sprintf(fmt, "%%.%ds", count)

Don used a "recursion-like" style and escaped the symbol %%, that I never used before. (By the way, wish there is a book to cover those tricks/exceptions that you can't find in K&R book.)Thank you both so much!

Last edited by yifangt; 01-18-2014 at 04:08 PM..
# 21  
Old 01-18-2014
Its good to know that you are happy ! I am sleeping now Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Pointer for 2D array seems to be 3D in C

I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y, especially the high lighted lines? I have talked to one of the curators of the forum, but I am still not quite clear. Here... (1 Reply)
Discussion started by: yifangt
1 Replies

2. Programming

Character pointer to Character array

how to copy content of character pointer to character array in c programming.. char *num; char name=num; (1 Reply)
Discussion started by: zinat
1 Replies

3. Shell Programming and Scripting

best practises for scripting + a few unclear points

Hi guys, Besides the points bellow, what would best practices for scripting be ? 1) set the PATH 2) unset the current environment (set -u ?) 3) (re)set the IFS to default value - space (IFS="" <- is this correct ?) 4) check the return code for each action inside the script (cd, rsync,... (1 Reply)
Discussion started by: da1
1 Replies

4. Programming

structure pointer array as function parameters

if i create an array of pointers to a structure "struct node" as: struct node *r; and create "n" number of "linked lists" and assign it to the various struct pointers r using some function with a return type as structure pointer as: r=multiplty(.......) /*some parameters*/ is... (2 Replies)
Discussion started by: mscoder
2 Replies

5. Programming

help with char pointer array in C

i have an array like #define NUM 8 .... new_socket_fd = accept(socket_fd, (struct sockaddr *) &cli_addr, &client_length); char *items = {"one", "two", "three", "four", "five", "six", "seven", "eight"}; char *item_name_length = {"3", "3", "5", "4", "4", "3", "5", "5"}; ... (1 Reply)
Discussion started by: omega666
1 Replies

6. Programming

C pointer/array duality confusion

Hi all, Can anyone provide help with getting the right syntax regarding array/pointers in C in the following code? Can't locate a specific example which clarifies this... Say I declare a typedef to an array of pointers to some type... /** * An array of ptrs to sections */ typedef... (4 Replies)
Discussion started by: gorga
4 Replies

7. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

8. UNIX for Dummies Questions & Answers

Storing pointer array in C

All .. I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced code: int cnt1; char *t_array; char *f_array; for(cnt1=0; cnt1<1000; cnt1++) { t_array =... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

9. Programming

pointer

void main() { int a={1,2,3,4,5,6,7,8,9,10}; int *p=a; int *q=&a; cout<<q-p+1<<endl; } The output is 10, how? if we give cout<<q it will print the address, value won't print.... if we give cout<<p it will print the address, value won't print.... p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies
Login or Register to Ask a Question