-3

Previous sloppy question editted out for clarity.

Edits: Here's a short version of the code I used. I apologize if I include some unnecessary information (I am not quite familiar with C).

Code:

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <math.h>
 #include <time.h>

#define SEQ 200
#define POS 100

char *copy_substr (char *source, int start, int end){
    int x,y;
    char target[1000];

    for (x=0; x<sizeof(target); x++)
            target[x]='\0';

    for (x=start, y=0; x <= end; x++, y++)
            target[y]=source[x];

    return(target);
}

main(){
char seq[SEQ][POS];
int no=0;
char garbage[1000];
int x,y,z,i,j;
int len;


/* Reads in alignment file from stdin */
while (fgets(garbage, sizeof(garbage), stdin)){
    x=0;

            /* Finds boundaries */
            while (garbage[x++] == ' ');
            while (garbage[x++] != ' ');
            while (garbage[x++] == ' ');
            y=--x;                        

    while(isalpha(garbage[x]) || garbage[x] == '.' || garbage[x] == '-')
        x++;
    x--;
    printf("*%d %d %d*",no,x,y);
    printf("%s",garbage);
    strcpy(seq[no], copy_substr(garbage, y, x));
    printf("%s\n",seq[no]);
    no++;
}
len=strlen(seq[0]);
}

Input file:

 4322822  PLP.......PG...WEIKIDPQTGWPFFVDHNSRTTTWNDPRVP
 3560257  GLP.......KP...WIVKISRSRNRPYFFNTETHESLWEPPAAT
 3046716  PLPNLGQSVKPP...WERATTAAN.VPYYIDHERQTTHWDHPEMI
 3875252  PLP.......SG...WECITMNN.RTVFLNHANKETSFYDPRIRR
2827198c  PLP.......EG...WEMRFTVD.GIPYFVDHNRRATTYIDPRTG
2708329c  PLP.......EG...WEMRFTVD.GIPYFVDHNRRTTTYIDPRTG
 3875841  PMP.......QG...WEMCYDSD.GVRYFKDHNSKTTTWDDPRLK
1171682d  GLP.......PG...WEEKQDER.GRSYYVDHNSRTTTWTKPTVQ
2072503d  ALP.......AG...WEQRELPN.GRVYYVDHNTKTTTWERPLPP
3411053b  ELP.......YG...WEKIDDPIYG.TYYVDHINRRTQFENPVLE

Output:

*0 58 14*     4322822  PLP.......PG...WEIKIDPQTGWPFFVDHNSRTTTWNDPRVP
|=҇
*1 58 14*     3560257  GLP.......KP...WIVKISRSRNRPYFFNTETHESLWEPPAAT
GLP.......KP...WIVKISRSRNRPYFFNTETHESLWEPPAAT
*2 58 14*     3046716  PLPNLGQSVKPP...WERATTAAN.VPYYIDHERQTTHWDHPEMI
PLPNLGQSVKPP...WERATTAAN.VPYYIDHERQTTHWDHPEMI
*3 58 14*     3875252  PLP.......SG...WECITMNN.RTVFLNHANKETSFYDPRIRR
PLP.......SG...WECITMNN.RTVFLNHANKETSFYDPRIRR
*4 58 14*    2827198c  PLP.......EG...WEMRFTVD.GIPYFVDHNRRATTYIDPRTG
PLP.......EG...WEMRFTVD.GIPYFVDHNRRATTYIDPRTG
*5 58 14*    2708329c  PLP.......EG...WEMRFTVD.GIPYFVDHNRRTTTYIDPRTG
PLP.......EG...WEMRFTVD.GIPYFVDHNRRTTTYIDPRTG
*6 58 14*     3875841  PMP.......QG...WEMCYDSD.GVRYFKDHNSKTTTWDDPRLK
PMP.......QG...WEMCYDSD.GVRYFKDHNSKTTTWDDPRLK
*7 58 14*    1171682d  GLP.......PG...WEEKQDER.GRSYYVDHNSRTTTWTKPTVQ
GLP.......PG...WEEKQDER.GRSYYVDHNSRTTTWTKPTVQ
*8 58 14*    2072503d  ALP.......AG...WEQRELPN.GRVYYVDHNTKTTTWERPLPP
ALP.......AG...WEQRELPN.GRVYYVDHNTKTTTWERPLPP
*9 58 14*    3411053b  ELP.......YG...WEKIDDPIYG.TYYVDHINRRTQFENPVLE
ELP.......YG...WEKIDDPIYG.TYYVDHINRRTQFENPVLE 

Expected output:

*0 58 14*     4322822  PLP.......PG...WEIKIDPQTGWPFFVDHNSRTTTWNDPRVP
PLP.......PG...WEIKIDPQTGWPFFVDHNSRTTTWNDPRVP
*1 58 14*     3560257  GLP.......KP...WIVKISRSRNRPYFFNTETHESLWEPPAAT
GLP.......KP...WIVKISRSRNRPYFFNTETHESLWEPPAAT
*2 58 14*     3046716  PLPNLGQSVKPP...WERATTAAN.VPYYIDHERQTTHWDHPEMI
PLPNLGQSVKPP...WERATTAAN.VPYYIDHERQTTHWDHPEMI
*3 58 14*     3875252  PLP.......SG...WECITMNN.RTVFLNHANKETSFYDPRIRR
PLP.......SG...WECITMNN.RTVFLNHANKETSFYDPRIRR
*4 58 14*    2827198c  PLP.......EG...WEMRFTVD.GIPYFVDHNRRATTYIDPRTG
PLP.......EG...WEMRFTVD.GIPYFVDHNRRATTYIDPRTG
*5 58 14*    2708329c  PLP.......EG...WEMRFTVD.GIPYFVDHNRRTTTYIDPRTG
PLP.......EG...WEMRFTVD.GIPYFVDHNRRTTTYIDPRTG
*6 58 14*     3875841  PMP.......QG...WEMCYDSD.GVRYFKDHNSKTTTWDDPRLK
PMP.......QG...WEMCYDSD.GVRYFKDHNSKTTTWDDPRLK
*7 58 14*    1171682d  GLP.......PG...WEEKQDER.GRSYYVDHNSRTTTWTKPTVQ
GLP.......PG...WEEKQDER.GRSYYVDHNSRTTTWTKPTVQ
*8 58 14*    2072503d  ALP.......AG...WEQRELPN.GRVYYVDHNTKTTTWERPLPP
ALP.......AG...WEQRELPN.GRVYYVDHNTKTTTWERPLPP
*9 58 14*    3411053b  ELP.......YG...WEKIDDPIYG.TYYVDHINRRTQFENPVLE
ELP.......YG...WEKIDDPIYG.TYYVDHINRRTQFENPVLE 

Please note that the copy_substr didn't work for the first line of the input (the second line of the output doesn't match the second line of the expected output).

Chenxi Qiu
  • 45
  • 3
  • 3
    It's very very unlikely to have anything directly to do with your version of macbook. It's much more likely you have bugs in your code. But we can't help you unless you provide a [mcve]. – kaylum Nov 17 '16 at 01:15
  • Suggestion: use `strncpy` instead of `strcpy` to avoid buffer overflow. Not saying this was your issue, as there aren't enough details, but it could be. – Brian Nov 17 '16 at 01:15
  • 2
    Without a [mcve], I would guess you have an undefined or unspecified behavior. Both will show differently on different environments. You might even have implementation defined behavior. – alvits Nov 17 '16 at 01:21
  • `copy_substr()` itself can cause this if not written well. – alvits Nov 17 '16 at 01:22
  • `copy_substr` returns a pointer to automatic storage. – joop Nov 17 '16 at 17:00
  • Thanks joop. Sorry my c knowledge is limited. Is there a simple fix for this? Should I change return(target) to something else? – Chenxi Qiu Nov 17 '16 at 17:02
  • The simplest is to let the caller (main) supply a buffer, and pass that as an extra argument to the copy function. – joop Nov 17 '16 at 17:06
  • Thanks joop. That solves my problem! – Chenxi Qiu Nov 17 '16 at 21:16

2 Answers2

0

I am actually not exactly sure if this is related to the mac OS updates, but I have a C code that works for a computer at 10.6 but not for the other two macbooks at 10.10 and 10.11.6.

C works the same way since it's creation. Computers read it the same way. like kaylum, i think the issue is in the code itself not the computers/versions/os/etc

kiwe
  • 11
  • 5
  • True, i'm just saying int garbage; garbage += 5; garbage is still garbage + 5... the standards don't add garbage ? And do correct me if i'm wrong i'm all about learning – kiwe Nov 17 '16 at 01:30
  • it seems i commented a guy who deleted his comment ? – kiwe Nov 17 '16 at 01:31
  • I removed my comment because this link is way better than my short comment http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior/4105123#4105123 – alvits Nov 17 '16 at 01:34
  • Thanks for the link and comments guys. Can you look at my editted question and see if I have sufficient information there now? – Chenxi Qiu Nov 17 '16 at 17:03
  • i can only say: if you want to avoid future errors like the one you encountered, NEVER expect anything from non initialized variables, DO NOT use static char arrays (in the case where your arrays can have many sizes), you are simply pushing a new issue that you'll have to resolve later if "char target[1000];" must contain one more char for example, to you want to recompile everything ? google "malloc" and "free" use them wisely, they are friends, not foes – kiwe Nov 17 '16 at 21:22
  • NB: it makes the difference between a program that works and rarely / never crashes and a program that works the same but you got that feeling in your gut every time you use it that all could go wrong in an instant and you could loose all your work/info/outputs/etc (hi windows) – kiwe Nov 17 '16 at 21:25
  • Thanks for the advice kiwe. Will look them up – Chenxi Qiu Nov 17 '16 at 22:25
0

You store copied string to stack allocated array char target[1000]. You need to heap allocate for that storage with malloc and friends and use that for return. Elements of existing target are not defined while and after copy_substr returning.

Lutfullah Tomak
  • 741
  • 4
  • 6