static void swap(int a, int b) { int tmp; printf("a = %d, b = %d\n", a, b); tmp = a; a = b; b = tmp; printf("a = %d, b = %d\n", a, b); // ←a = 20, b = 10になるが }
int main(void) { int a = 10, b = 10; printf("a = %d, b = %d\n", a, b); swap(a, b); printf("a = %d, b = %d\n", a, b); // ←a = 20, b = 10にはならない return 0; }
337 :デフォルトの名無しさん :2001/03/01(木) 15:36
すばやいアドバイスほんとにありがとうございます。
でも上の例って、、、 passing by value − サブファンクションにコピーを作る のと同じではないでしょうか?
struct ABC{ int aaa; int bbb; int ccc; }; って構造体と文字列char sz[10]があって、 szが"aaa"のときはABCのメンバaaaを処理 szが"bbb"のときはABCのメンバbbbを処理 szが"ccc"のときはABCのメンバcccを処理 みたいに、その文字列が表している構造体のメンバを参照したいのですが、可能でしょうか?
void unpack(const unsigned char *dat, const char *fmt, ...) { char *cp; int *np; long *lp;
int i; unsigned int n; va_list ap;
va_start(ap, fmt); for (; *fmt != '\0'; ++fmt) switch (*fmt) { case 'c': cp = va_arg(ap, char *); for (i = *++fmt - '0'; i > 0; i -= 2) { n = *dat++; *cp++ = ntoc(n >> 4); *cp++ = ntoc(n & 0xf); } break; case 'd': np = va_arg(ap, int *); *np = 0; for (i = 0; i < 2; ++i) { *np <<= 8; *np |= *dat++; } break; case 'l': lp = va_arg(ap, long *); *lp = 0; for (i = 0; i < 4; ++i) { *lp <<= 8; *lp |= *dat++; } break; }
va_end(ap); }
int main(void) { char buf[] = { 0x30, 0x31, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc }; char c[4]; int i; long l;
unpack(buf, "c4dl", &c[0], &i, &l); printf("c = %.4s, i = %X, l = %lX\n", &c[0], i, l); return (0); }
511 :498 :2001/03/30(金) 07:54
じゃ構造体を使う移植性のあるやつを。
#include <limits.h>
struct T { char c[2]; char i[2]; char l[4]; };
long getint(const char* p, int n) { long x = (signed char)p[0]; int i; for (i = 1; i < n; ++i) { x = (x << CHAR_BIT) | (unsigned char)p[i]; } return x; }
#define intof(x) getint((x), sizeof(x))
int main(void) { static const char buf[] = {0x30, 0x31, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc}; const struct T* t = (const struct T *)buf; printf("c = %.*s, i = %X, l = %lX\n", sizeof(t->c), t->c, intof(t->i), intof(t->l)); return 0; }
自分もアホです。低次元な質問につきあってやって下さい。"C for Dummies"っちゅう入門書を読み始めんですけど、"DOS"だの "DOS at prompt"だのに苦しめられてて、C言語習得のスタートラインにすら立てません。DOSとはいったいなんのことですか?これ知ってないと話になんないの?よきアドバイスを。
Thank you for pointing out! You correctly spotted the cause of the error.I have compiled the source code. Though I look up my text,I can't find out a need of "a whitespace". I have been learnig on C programing.----beginner.
>Tte,nande eigo nanjiagoruaa!! Sorry,I am poor at Japanese.What meaning? MORE THANKS.
def hittest( str , index ) $map_text.each { | i | next if str[index, i[0].length] != i[0] i[2] += 1 return [true, i[1], i[0].length] } return [false] end
out = ""; while (ARGF.gets) chop! i=0 while( i < $_.length ) ret = hittest($_, i) if ret[0] then out += ret[1] i += ret[2] else i+=1; out += '.' end end end
i=0 while( i < out.length ) print out[i, 20],"\n" i += 20 end