Sponsored Content
Full Discussion: Unclear pointer and array
Top Forums Programming Unclear pointer and array Post 302884201 by Akshay Hegde on Saturday 18th of January 2014 09:46:58 AM
Old 01-18-2014
Hi yifangt try this simplified version

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

char *substring(size_t start, size_t stop, const char *src)
{
    
    unsigned int count = stop - start;
    char *dst=malloc(stop+1);
    sprintf(dst, "%.*s", count, src + start);
    return dst;
}

int main(int argc, char **argv)
{
    if (argc != 4) {
    printf("Error! Usage:\n\t \
            argv[0]=program;\n\t \
            argv[1]=input string\n\t \
            argv[2]=start_position of string\n\t \
            argv[3]=end_postion of string\n");

    return 1;
    }

    printf("%s\n",substring(atoi(argv[2]), atoi(argv[3]), argv[1]));
    return 0;
}

This User Gave Thanks to Akshay Hegde 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
GETFLAGS(9.2)															     GETFLAGS(9.2)

NAME
getflags, usage - process flag arguments in argv SYNOPSIS
#include <libg.h> #include <fb.h> int getflags(int argc, char *argv[], char *flags) int usage(char *tail) extern char **flag[], cmdline[], *cmdname, *flagset[]; DESCRIPTION
Getflags digests an argument vector argv, finding flag arguments listed in flags. Flags is a string of flag letters. A letter followed by a colon and a number is expected to have the given number of parameters. A flag argument starts with `-' and is followed by any number of flag letters. A flag with one or more parameters must be the last flag in an argument. If any characters follow it, they are the flag's first parameter. Otherwise the following argument is the first parameter. Subsequent parameters are taken from subsequent arguments. The global array flag is set to point to an array of parameters for each flag found. Thus, if flag -x was seen, flag['x'] is non-zero, and flag['x'][i] is the flag's ith parameter. If flag -x has no parameters flag['x']==flagset. Flags not found are marked with a zero. Flags and their parameters are deleted from argv. Getflags returns the adjusted argument count. Getflags stops scanning for flags upon encountering a non-flag argument, or the argument --, which is deleted. Getflags places a pointer to argv[0] in the external variable cmdname. It also concatenates the original members of argv, separated by spaces, and places the result in the external array cmdline. Usage constructs a usage message, prints it on the standard error file, and exits with status 1. The command name printed is argv[0]. Appropriate flag usage syntax is generated from flags. As an aid, explanatory information about flag parameters may be included in flags in square brackets as in the example. Tail is printed at the end of the message. If getflags encountered an error, usage tries to indi- cate the cause. EXAMPLES
main(int argc, char *argv[]){ if((argc=getflags(argc, argv, "vinclbhse:1[expr]", 1))==-1) usage("[file ...]"); } might print: Illegal flag -u Usage: grep [-vinclbhs] [-e expr] [file ...] SOURCE
/sys/src/libfb/getflags.c SEE ALSO
ARG(2) DIAGNOSTICS
Getflags returns -1 on error: a syntax error in flags, setting a flag more than once, setting a flag not mentioned in flags, or running out of arguments while collecting a flag's parameters. GETFLAGS(9.2)
All times are GMT -4. The time now is 08:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy