Another Understanding Pointers Question
From: Materialised (Materialised_at_privacy.net)
Date: 05/10/04
- Next message: Leor Zolman: "Re: Referencing a global array outside a function"
- Previous message: SM Ryan: "Re: change where a file pointer points, inside a function"
- Next in thread: Leor Zolman: "Re: Another Understanding Pointers Question"
- Reply: Leor Zolman: "Re: Another Understanding Pointers Question"
- Reply: Darrell Grainger: "Re: Another Understanding Pointers Question"
- Reply: Robert Bachmann: "Re: Another Understanding Pointers Question"
- Reply: Joe Wright: "Re: Another Understanding Pointers Question"
- Reply: Materialised: "Re: Another Understanding Pointers Question"
- Reply: Materialised: "Re: Another Understanding Pointers Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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;'
- Next message: Leor Zolman: "Re: Referencing a global array outside a function"
- Previous message: SM Ryan: "Re: change where a file pointer points, inside a function"
- Next in thread: Leor Zolman: "Re: Another Understanding Pointers Question"
- Reply: Leor Zolman: "Re: Another Understanding Pointers Question"
- Reply: Darrell Grainger: "Re: Another Understanding Pointers Question"
- Reply: Robert Bachmann: "Re: Another Understanding Pointers Question"
- Reply: Joe Wright: "Re: Another Understanding Pointers Question"
- Reply: Materialised: "Re: Another Understanding Pointers Question"
- Reply: Materialised: "Re: Another Understanding Pointers Question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|