Sponsored Content
Full Discussion: Unclear pointer and array
Top Forums Programming Unclear pointer and array Post 302884213 by Don Cragun on Saturday 18th of January 2014 12:45:20 PM
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:
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
ASPRINTF(3)						     Linux Programmer's Manual						       ASPRINTF(3)

NAME
asprintf, vasprintf - print to allocated string SYNOPSIS
#define _GNU_SOURCE #include <stdio.h> int asprintf(char **strp, const char *fmt, ...); int vasprintf(char **strp, const char *fmt, va_list ap); DESCRIPTION
The functions asprintf and vasprintf are analogues of sprintf and vsprintf, except that they allocate a string large enough to hold the output including the terminating NUL, and return a pointer to it via the first parameter. This pointer should be passed to free(3) to release the allocated storage when it is no longer needed. RETURN VALUE
When successful, these functions return the number of bytes printed, just like sprintf(3). If memory allocation wasn't possible, or some other error occurs, these functions will return -1, and the contents of strp is undefined. NOTES
These functions are GNU extensions, not in C or POSIX. They are also available under *BSD. The FreeBSD implementation sets strp to NULL on error. SEE ALSO
free(3), malloc(3), printf(3) GNU
2001-12-18 ASPRINTF(3)
All times are GMT -4. The time now is 06:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy