   C coding problem...
  Hi there,Could anybody help me with some C coding? I''d like to make a function similar to MID in VB or SUBSTR in php. You know, you pass\a string, start position, end position and the function returnsonly specified part of the passed string. like mid("Joseph",2,5)should return "seph" or "ose" depending on indexing. I''ve beenbothering with effort to build that, but no result until now.If you could help me how to build it or where to find such a thing,please, let me know at joseph_drake@centrum.sk.Thanks a lot 
  I dont know VB or PHP but you can just store the string in an array and access whatever part of it you like.   An ASCII tetris clone... | AsciiRis;  that''s what I exactly did! I put the code into the function and returned the substring. the problem is that the function returnsthe pointer to the first char, and I cannot make any move from this, I''m stuck here. ;  It shouldn''t be too difficult to come up with a version of substr() in C, just so long as you''ve thought about the mechanics of the problem that you''re trying to solve.  It''s very easy to get into the habit of, "It''s just one small function.  No need to design a solution to the problem.", which isn''t always the case.Having a quick look through Google, gave me the following snippet:http://www.1phpstreet.com/vb/scripts/ShowCode.asp?txtCodeId=6323&lngWId=3Whilst I''ve not tested to see exactly how it works, I knew straight away I would have done it differently (the return type of void, indicated that).Have a play with it, and see if it does what you require.  If it does, perhaps you could come up with a version that would be more suitable and behave more like the substr() function people have been so used to using in scripting languages.--hellz ;quote:Original post by joseph drake  that''s what I exactly did! I put the code into the function and returned the substring. the problem is that the function returnsthe pointer to the first char, and I cannot make any move from this, I''m stuck here.   Want to post some code for us to take a look at?--hellz ;  Can you post the code in the function and the array definition/initialization?   An ASCII tetris clone... | AsciiRis;  have a look at it then. the code is full of "remmed" parts...#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <string.h>char mid;char substring(char *string, int start, int end){ int c1; char *str; char *substr; int len; len = end-start-1; //printf("%s\n",string); str=(char*)malloc(strlen(string)); substr=(char*)malloc(6); str=string; c1=0; //substr=""; printf("%s\n",str); while (c1<=end) {  substr[c1]=str[start++];  printf("%c\n",substr[c1]);  c1++;  } //mid=substr;  //printf("lenght of string is %i",strlen(substr));  //printf("%s\n",*substr);  return *substr;}void main(void){//FILE *source;//char *temp;char *a;//temp=NULL;clrscr();//printf("joseph from 1 to 3\n");a = (char*)substring("joseph",0,3);printf("ehn %c\n", &a);free(substring);};char* substr( char* szOut, const char* szIn, int iBegin, int iEnd ){   char* p = szOut;   for ( int i = iBegin; i < iEnd + 1 && szIn[ i ]; ++i )   { *( p++ ) = szIn[ i ];  // Copy character   }   *p = 0;  // Zero terminate new string   return szOut;}Will that do? Would return ''seph'' in your example ;quote:Original post by joseph drake  have a look at it then. the code is full of "remmed" parts...#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <string.h>char mid;char substring(char *string, int start, int end){ int c1; char *str; char *substr; int len; len = end-start-1; //printf("%s\n",string); str=(char*)malloc(strlen(string)); substr=(char*)malloc(6); str=string; c1=0; //substr=""; printf("%s\n",str); while (c1<=end) {  substr[c1]=str[start++];  printf("%c\n",substr[c1]);  c1++;  } //mid=substr;  //printf("lenght of string is %i",strlen(substr));  //printf("%s\n",*substr);  return *substr;}void main(void){//FILE *source;//char *temp;char *a;//temp=NULL;clrscr();//printf("joseph from 1 to 3\n");a = (char*)substring("joseph",0,3);printf("ehn %c\n", &a);free(substring);} Ok, there''s a few things I''ve noticed (just by having a quick read through).char *a;//temp=NULL;clrscr();//printf("joseph from 1 to 3\n");a = (char*)substring("joseph",0,3);printf("ehn %c\n", &a);  As you can see, "a" is a pointer of type char.  You should also be aware that the name of an array, is a constant pointer to the first element of that array.  With that in mind, the following works:char string[] = "hellz";printf("Who am I: %s", string);  Notice that I''ve not used the address-of operator (&) to print the string.  In your last printf() statement, you are printing the address of the pointer (which is an integer), which is then converted to be output as a character (because you have used the character format string within printf - %c).Change:printf("ehn %c\n", &a);  To:printf("ehn %s\n", a);  Also, you need to change the function definition, as it''s return type is "char", but you''re not returning a pointer to one, which is what you want in this case; this is also the reason you''re having to cast the result stored in "a", using (char *).  So, change the function to this:char *substring(char *string, int start, int end)  The * before substring, indicates that you want to return a pointer from the function.  If you read the function from right-to-left, it''s effectively saying, "substring wishes to return a pointer to type char."One other thing to mention, the main() function, has a return type of "int", not "void".  The operating system expects an integer to be returned from programs, to indicate if they were executed successfully or otherwise.  The following is valid:int main(){   // some code goes here   return 0; // Return a value to the OS.  0 represents successful execution.}  --hellz ;  didn''t get it well yet, what''s that 2nd parameter for?