Pointers & char arrays

This is a discussion on Pointers & char arrays within the C Programming forums, part of the General Programming Boards category; I'm triying to make a function to get something from the middle of another 2 strings, this is what I've ...

  1. #1
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193

    Question Pointers & char arrays

    I'm triying to make a function to get something from the middle of another 2 strings, this is what I've got..
    The code goes trough the string using it's lenght in a for loop, while it goes trough, it starts finding the letters from the first item and also increments j, to look for the other letter and if it is not found, it resets it, then if j is equal to the first item's lenght, it continues with the second one and it does the same, but if it is equal to the second item lenght it returns the string in between
    Code:
    char *getFromMiddle(char *string, int l_string, char *first, int l_first, char *second, int l_second) { // the function has to return a char pointer, is it well declared?
    	int i, j, x, f_pos, s_pos, l_middle;
    	char t, *middle;
    	printf("Declara variables \n");
    		for(i=0;i<l_string;i++) {
    			printf("For numero: %i \n", i);
    			if(x != 1) {
    				if(*string[i] == *first[j]) { //trouble here
    					printf("Igualdad entre i nš=%i(%c) con j nš=%i(%c): %i \n", i, string[i], j, first[j]);
    					j++;
    					if(j == l_first) {
    					printf("J(%i) = l_first(%i)",j ,i);
    						f_pos = i;
    						j = 0;
    						x = 1;
    					}
    				}
    				else {
    					printf("No se encontro igualdad en i->%i(%c) == j->%i(%c)",i ,string[i], j, first[j]);
    					j = 0;
    				}
    			}
    			else {
    				if(*string[i] == *second[j]) { // also here
    					j++;
    					if(j == l_second) {
    						s_pos = i-l_second;
    						l_middle = s_pos-f_pos;
    						middle = malloc(sizeof(t)*(l_middle+1));
    						for(x=0;x<l_middle;x++) {
    							*middle[x] == *string[f_pos];
    							f_pos++;
    						}
    						*middle[l_middle+1] = '\0';
    						return middle; // is it middle well returned?
    					}
    				}
    				else {
    					j = 0;
    				}
    			}
    		}
    	return 0; // return 0 if nothing was found
    }
    I'm having troubles with the pointers and the char arrays, how can I use them as a classic char array? Also I don't really know if the return variable will be a char pointer..
    Thanks
    Last edited by lautarox; 05-13-2009 at 12:12 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lautarox View Post
    I'm having troubles with the pointers and the char arrays, how can I use them as a classic char array? Also I don't really know if the return variable will be a char pointer..
    Thanks
    What do you mean by "classic char array"? And why are you dereferencing everywhere:
    Code:
    if(*string[i] == *first[j]) { //trouble here
    English is not your first language so I will let you know: this sentence makes no sense, so it's hard to decide what your intent is:
    I'm triying to make a function to get something from the middle of another 2 strings
    You never set j (or x) to 0, either, meaning they could be anything from the start. I would say this code obviously does not work at all, and you are making a big mistake writing so much without *any* of it working. It is a *very bad idea* to just write a huge block like this presuming it will all work before you try and compile even once. You should break the task down into smaller steps so that you can write something simple, make sure it works the way you intend, and then get more complicated.
    Last edited by MK27; 05-13-2009 at 12:35 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    English is not your first language so I will let you know: this sentence makes no sense, so it's hard to decide what your intent is:
    You are right =P
    I'm triying to make a function, to get a string from the middle of two items, like <hello>asas</hello>, the function should get asas.
    I mean like a "classic" -> normal char array, is it possible to use the pointer with the [] tags as the char array?
    By the way, thanks for your time
    Last edited by lautarox; 05-13-2009 at 12:43 PM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by lautarox View Post
    You are right =P
    I'm triying to make a function, to get a string from the middle of two items, like <hello>asas</hello>, the function should get asas.
    I mean like a "classic" -> normal char array, is it possible to use the pointer with the [] tags as the char array?
    By the way, thanks for your time
    Yep! it is possible to use [] as an alternative to the pointer "->" notation, though when there's structured HTML text like this one strtok() is a better choice in picking out pieces of the line based on the "<" or ">" delimiters.

  5. #5
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Yep! it is possible to use [] as an alternative to the pointer "->" notation
    How could it be used? an example will be appreciated.
    I'll try using strtok()
    Edit*, I won't be able to use strtok(), because of the large ammount of tags in the string..
    Last edited by lautarox; 05-13-2009 at 01:08 PM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lautarox View Post
    How could it be used? an example will be appreciated.
    I'll try using strtok()
    Here's a short example that might help if you also read some documentation:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	char string[]="<h1>Example</h1>",*tok;
    	tok=strtok(string,"<>");
    	printf("%s\n",tok);
    	while ((tok=strtok(NULL,"<>"))) printf("%s\n",tok);
            printf("Now string=%s\n",string);
    	return 0;	
    }
    Notice that the *first* strtok() uses "string", and after that NULL since this is already set. Also be aware that this does strange things to "string" itself, which is shown with the last printf.

    That may or may not be useful to you, depending on the context. Your original idea is not a bad one, you just need to work it out more slowly and always try and keep it so that you can compile and test (with printf for debugging) every couple of lines -- even if this means making small simplifications that are not permanent.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    Thanks MK27, but I think that strtok() won't work because of the large amount of tags the string has, could you tell me how could I access the value of a character from the pointer string using the [] tags? that's what I need to continue debugging the code.
    Example:
    // ptr is a pointer, and so it is ptr2
    I want it to point to the (1) character, it would be the second counting from the 0, so it would compare the value they have.
    if(ptr[1] == ptr2[1]) { ...
    Last edited by lautarox; 05-13-2009 at 01:36 PM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lautarox View Post
    Thanks MK27, but I think that strtok() won't work because of the large amount of tags the string has, could you tell me how could I access the value of a character from the pointer string using the [] tags? that's what I need to continue debugging the code
    strtok() will work on a longer string too, but here's something that demonstrates the proper syntax for passing a C string and accessing it's individual elements (characters):
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void tagsout (char *ptr) {
    	int len = strlen(ptr), i, j=0, tag=0; /* "tag" is a flag */
    	char tmp[len];
    	for (i=0; i<len; i++) {
    		if (ptr[i]=='<') tag=1;
    		if (ptr[i]=='>') { tag=0; continue; }
    		if (tag) continue;  /* flag is set, so skip */
    		tmp[j]=ptr[i]; j++;  /* or add to tmp and advance j counter */
    	}
    	tmp[j]='\0'; /* null terminate replacement */
    	strcpy(ptr,tmp);
    }
    		
    
    int main() {
    	char string[]="<div class=\"this\"><p>Hello</p><center><h1>Example</h1></center></div>";
    	tagsout(string);
    	printf("%s\n",string);
    	return 0;	
    }
    Hopefully that will at least get you started...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    lol, I don't know why I used the *'s , It worked almos perfect without the *'s, now I'm having a problem here..
    Code:
    					if(j == l_second) {
    						s_pos = i-l_second;
    						l_middle = s_pos-f_pos;
    						printf("J(%i) = l_second(%i) s_pos = %i , f_pos = %i , l_middle = %i \n",j ,i, s_pos, f_pos, l_middle);
    						int test;
    						test = sizeof(t)*(l_middle+1);
    						printf("test = %i \n", test);
    						middle = malloc((sizeof(t))*(l_middle+1));
    						if(middle == NULL) {
    							printf("No se alloco \n");
    							exit(1);
    						}
    						for(x=0;x<l_middle;x++) {
    							middle[x] == string[f_pos];
    							f_pos++;
    						}
    						middle[l_middle+1] = '\0';
    						printf("Termina middle = %s \n", middle);
    						return middle;
    					}
    It returns:
    test = 7
    Termina middle = -> why middle is not beeing printed?

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Instead of comparing for equality shouldn't this be an assignment with a single equals sign, as in
    Code:
    middle[x] = string[f_pos];

  11. #11
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    lol, thanks, it worked perfectly!

  12. #12
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    second solution, it does for specific tags only
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21