Unclear pointer and array


 
Thread Tools Search this Thread
Top Forums Programming Unclear pointer and array
# 8  
Old 01-18-2014
Quote:
Originally Posted by yifangt
Yes, got it!
Want to catch the pointer from the example. After I changed to:
Code:
substring(start, end, text, a, strlen(a)));

seems working, but need confirm if I did in the correct way.
That doesn't sound right. Please show us all of your current code.
# 9  
Old 01-18-2014
Here it is:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *substring(size_t start, size_t stop, const char *src, char *dst)
{
    unsigned int count = stop - start;
    size_t size = strlen(dst);

    if (count >= --size) {
    count = size;
    }
    sprintf(dst, "%.*s", count, src + start);
    return dst;
}

int main(int argc, char **argv)
{
    char *text=(char*)malloc(100*sizeof(char));  //Line 40, change to text[100] will work
    char *a=(char*)malloc(100*sizeof(char));    //Line 41, change to a[100] will work
    int start, end;

    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;
    }

    strcpy(text, argv[1]);
    start = atoi(argv[2]);
    end = atoi(argv[3]);

    printf("substring = \"%s\"\n", substring(start, end, text, a));

    return 0;
}

Rewrote the substring() function as compared with the original version which may only handle string array specifically.
I came to the dead corner of my knowledge on string pointer in C: How to get the string length when you dynamically allocate the memory space for it? Or after you allocate the memory of a string pointer, how to get the string length?

Last edited by yifangt; 01-18-2014 at 09:13 AM.. Reason: Add more information.
# 10  
Old 01-18-2014
Try this instead:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define IOBufLen        100

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

int main(int argc, char **argv)
{
    char *text=(char*)malloc(IOBufLen);  //Line 40, change to text[100] will work
    char *a=(char*)malloc(IOBufLen);   //Line 41, change to a[100] will work
    int start, end;

    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;
    }

    strncpy(text, argv[1], IOBufLen - 1); // -1 to be sure text is null terminated.
    start = atoi(argv[2]);
    end = atoi(argv[3]);

    printf("substring = \"%s\"\n", substring(start, end, text, a, IOBufLen));

    return 0;
}

Do you see why these changes make it work?
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 01-18-2014
Thanks.
Does your code waste some memory for a if you have
Code:
#define IOBufLen        100

for the print out line:
Code:
printf("substring = \"%s\"\n", substring(start, end, text, a, IOBufLen));

as a is only the substring of text? In another way,
Code:
strncpy(text, argv[1], IOBufLen - 1); // -1 to be sure text is null terminated.

does this line always allocate 100 bytes to text even text is only 10 char long?
I am concerned if to process 100 millions of lines (my sequences).

Last edited by yifangt; 01-18-2014 at 09:50 AM..
# 12  
Old 01-18-2014
It wastes exactly the same amount of space your code wasted. My changes to your code just made changes to keep you from overwriting data following the space pointed to by text if your input string is longer than the space you allocated for your buffer.

Are you going to keep 100 millions of lines of data in memory at once, or are you going to process a line at a time? If you are processing a line at a time, wasting something less than 100 bytes is absolutely nothing to worry about.

If you're going to try to malloc() 100 million buffers for your output substrings, you need to reconsider lots of issues as you design code to deal with that much data. (Note that you're talking about 800,000,000 bytes just to hold the pointers to your buffers!) Obviously, you could allocate space for your output buffer in your substring() function based on the length of the string it is actually going to store into that buffer.
This User Gave Thanks to Don Cragun For This Post:
# 13  
Old 01-18-2014
Yes,
Quote:
If you're going to try to malloc() 100 million buffers for your output substrings, you need to reconsider lots of issues as you design code to deal with that much data. (Note that you're talking about 800,000,000 bytes just to hold the pointers to your buffers!)
but I do not know how to at this moment. Thanks a lot!
# 14  
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:
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