Another Understanding Pointers Question

From: Materialised (Materialised_at_privacy.net)
Date: 05/10/04


Date: Mon, 10 May 2004 15:11:42 +0100

Hi everyone,
I seen the post by Rob Morris, and thought that I would double check
that I was using pointers in the correct way. So I written the following
string functions to test. I know soem can be iumplimented using the
standard libary, but I just wanted to test writing my own functions.
They work ok, but I would like some feed back on any issues you can see
with them etc

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

char *left(char *string, int count)
{
        char *p;
        int i;
        p = malloc((count * sizeof(char)+1));
        if(!p){
                printf("Cannot allocate memory\n");
                exit(1);
        }

        for(i = 0; i <= count -1; i++) {
                p[i] = string[i];
        }
        p[i++] = '\0';

        return(p);

}

char *right(char *string, int count)
{
        char *p;
        int len, i, j = 0;
        p = malloc((count * sizeof(char)+1));
        if(!p){
                printf("Cannot allocate memory\n");
                exit(1);
         }
        len = strlen(string);
        for(i = (len - count); i <= len; i++){
                p[j] = string[i];
                j++;
        }
        
        p[j++] = '\0';
        return(p);
                        
}

char *chreplace(char *string, int count, char rep)
{
        char *p;
        p = malloc((sizeof(string)+1));
        if(!p){
                printf("Cannot allocate memory\n");
                exit(1);
        }
        count--;
        strcpy(p, string);
        p[count] = rep;
        
        return(p);
}

char *section(char *string, int from, int to)
{
        char *p;
         int i, j = 0;
        
        p = malloc(((to - from) * sizeof(char)+1));
         if(!p){
                 printf("Cannot allocate memory\n");
                 exit(1);
         }
        for( i = from; i <= to; i++) {
                p[j] = string[i];
                j++;
        }
        p[j++] = '\0';

        return(p);
}
int main(void)
{
        char blah[] = "abcdefghijklm";
        char *test;
        char *test2;
        char *test3;
        char *test4;
        
        test = left(blah, 10);
        test2 = right(blah, 10);
        test3 = chreplace(blah, 2, 'Q');
        test4 = section(blah, 4, 10);
        puts(test);
        puts(test2);
        puts(test3);
        puts(test4);
        return 0;
}

Comments and improvements are welcome, flames to if appropriate.

-- 
------
Materialised
perl -e 'printf "%silto%c%sck%ccodegurus%corg%c", "ma", 58, "mi", 64, 
46, 10;'


Relevant Pages

  • Re: socket communication: send & receive doesnt work right
    ... So I don't want to send a string as bytes. ... public void send_doubles(double vals, int len) throws ... // send a short acknowledgement to the server ... char *result; ...
    (microsoft.public.win32.programmer.networks)
  • Re: [PATCH] markers: modpost
    ... pointers to the name/format string pairs. ... The same can then be done with modules using the __markers section. ... +static void read_markers(const char *fname) ... int main ...
    (Linux-Kernel)
  • Re: [PATCH] markers: modpost
    ... This adds some new magic in the MODPOST phase for CONFIG_MARKERS. ... will be a neighbor of its format string. ... +static void read_markers(const char *fname) ... int main ...
    (Linux-Kernel)
  • Re: Is this code totaly a shit?
    ... | void UppStrg(char *Low, char *Upp, int cnt); ... whitespace-delimited string. ... You're also assuming that the representations of the characters ...
    (comp.lang.c)
  • Re: A string collection abstract data type
    ... duplicate string in Insert, InsertAt, ReplaceAt; ... used int instead of size_t for count and size for consistency with API. ... char *(StringCollection *SC, int idx, ... static int IsReadOnly; ...
    (comp.lang.c)