LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-01-2005, 03:09 AM   #1
ghaefb
Member
 
Registered: Aug 2004
Location: Slovenia
Distribution: Fedora Core
Posts: 37

Rep: Reputation: 15
C++ remove charachters from array


Ok, it there a simple way to remove for example first few or the last few charachters from normal c++ array ?

Let's say I have: char a[20] = "test 123";
I want to remove the "test", so only "123" will be stored in array.

I did it like this:
Code:
char temp[20]:
int i = 0, j = 4;
while(line[i] != '\0')
{
  temp[i] = a[j];
  i++; j++;
}
But this doesn't seem like a good way... segfault risk.
Is there a better way, more elegant maybe ???

Thanks
 
Old 06-01-2005, 05:20 AM   #2
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
My C++ is a bit rusty, but I'll give it a try:
Code:
/* 
	Chop out part of a string
	Infinity42 - 1-6-05 
*/

#include <string.h>
#include <stdio.h>
using namespace std;

int main() {
	char *in, *out;
	int start, stop, str_len;
	str_len=50;
	//use a safe way of getting the string here
	in=new char[str_len];
	strncpy(in,"A random string\0",15);
	//Get start and stop here
	start=2; stop=5;
	//probably should do more checking they are valid here
	if(start>strlen(in)||stop>strlen(in)||start<0||stop<0||(stop-start)<=0) {
		printf("Start or stop out of range\0");
		return -1;
	}
	out=new char[(stop-start)+1]; //leave an extra char for \0
	for(int i=start;i<=stop;i++) {
		strncat(out,&in[i],1);
	}
	strcat(out,"\0");
	printf("%s (%i)",out,strlen(out));
        delete[] out,in;
}
Hope that helps
 
Old 06-01-2005, 07:35 AM   #3
ghaefb
Member
 
Registered: Aug 2004
Location: Slovenia
Distribution: Fedora Core
Posts: 37

Original Poster
Rep: Reputation: 15
Ok, this looks complicated.
Thanks for the help, bu I need someting more simple and I'm trying to use as less string functions as I can.

My code works, I'm just not sure if it's safe...
 
Old 06-01-2005, 08:18 AM   #4
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
ah right, ok then. One way of making it safer it to test that you're not overunning the bounds, so:
Code:
char temp[20];
int i = 0, j = 4;
while(line[i] != '\0' && i<=20)
{
  temp[i] = a[j];
  i++; j++;
}
This will stop you running off the end of tmp[], but not a[]. Where are line[] and a[] coming from? If you could post a bigger fragment I might be able to help more.

Hope that helps
 
Old 06-01-2005, 08:37 AM   #5
ghaefb
Member
 
Registered: Aug 2004
Location: Slovenia
Distribution: Fedora Core
Posts: 37

Original Poster
Rep: Reputation: 15
I see... Thanks this helps
My mistake, the code is: (example)
Code:
char line[150];
std::ifstream text("/proc/version");
text.getline(line,150,'\n');

char temp[20];
int i = 0, j = 4;
while(line[i] != '\0' && i<=20)
{
  temp[i] = line[j];
  i++; j++;
}
text.close();
So now the temp array contains what I need.

One more question.... it's a general question about arrays.
I see people using "char *line;" more often than 'char line[100];"
So should I use pointer array ? Why ?
Because I can delete it at the end maybe ??

Thanks alot

Last edited by ghaefb; 06-01-2005 at 08:38 AM.
 
Old 06-01-2005, 09:31 AM   #6
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
ah, that makes more sense
Should you be checking
Code:
line[j] != '\0'
not
Code:
line[i] != '\0'
I've just thought: you could do this (hacky and probably bad practice)
Code:
char line[150];
std::ifstream text("/proc/version");
text.getline(line,150,'\n');

char *temp;
temp=&line[4]
text.close();
To explain how this works I will also answer your questions about array pointers.

An array in C is simply a pointer to the start of it. So 'line' on it's own is a pointer to the value 'line[0]'. Using var[x] is equivalent to *(var+x). So &var[x] really just gives you var+x, but i think using the referencing operator looks neater. So what you have now is a pointer to the fourth element in the array. Just bear in mind that if you change 'line' you will change temp to, as they both point to the same section of memory. So in answer to your question: an array is just a reserved section of contiguous memory, and the array is just a pointer to the start of that memory. The [] operators just dereference a piece of the memory. This is why it is so easy to segfault when dealing with arrays, as var[10000] will just blindly try to do *(var+10000). I'm not good at explaining things, so i hope that made some sense.. just keep asking me questions until you understand it lol

Hope that helps...
 
Old 06-01-2005, 09:35 AM   #7
ghaefb
Member
 
Registered: Aug 2004
Location: Slovenia
Distribution: Fedora Core
Posts: 37

Original Poster
Rep: Reputation: 15
I think I got it in some way...

So to sum up, I should use array pointers ?
 
Old 06-01-2005, 10:20 AM   #8
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
It is typically considered better programming practice to use dynamic arrays (aka, create a pointer and new the memory) then static arrays. For simple things like this there is really no reason not to do it your way, but if there is any chance you ever might want to grow your array you really need to allocated it dyamically.

Quite honestly though, I've gotten into the habit of using std::vector<Type> instead of arrays in cases where I have no way of determining a logical starting size for the array.

Last edited by jtshaw; 06-01-2005 at 10:21 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
array c++ harrylee2003 Programming 3 10-14-2005 05:04 PM
Array help DropHit Linux - Software 0 06-30-2005 09:31 PM
Foreign Charachters geekzen Linux - General 0 04-16-2004 04:26 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
Samba and unicode/international charachters cdxz Linux - Software 0 09-18-2003 12:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration