ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
/*
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;
}
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.
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 ??
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.