Ocena wątku:
- 0 Głosów - 0 Średnio
- 1
- 2
- 3
- 4
- 5
|
Zasięg zmiennych czy źle napisana funkcja?
|
| Autor |
Wiadomość |
WebNuLL
Linux support
Liczba postów: 1766
Dołączył: Sep 2009
Reputacja: 45
€: 4127
OS: Linux Gentoo
|
Zasięg zmiennych czy źle napisana funkcja?
Witam, dopiero zaczynam naukę języka "C", zainstalowałem środowisko IDE - Anjuta i szczerze przyznam, że to najlepsze IDE jakie widziałem.
Jednak mój problem leży po stronie kodu, a mianowicie coś jest nie tak z zasięgiem zmiennych.
Cytat:/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.c
* Copyright © WebNuLL 2010 <#@gmail.com>
*
* eps is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* eps is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
// substring function by dwhitney67
// http://www.linuxquestions.org/questions/...-c-432620/
char* substring(const char* str, size_t begin, size_t len)
{
if (str == 0 || strlen(str) == 0 || strlen(str) < begin || strlen(str) < (begin+len))
return 0;
return strndup(str + begin, len);
}
int main(int argc, char* argv[])
{
// keep in mind that argv[0] is the program name so I skip it
int i;
char* tmpString;
for (i = 1; i < argc; ++i)
{
if (argv[i][0] == '-') // looks like an argument option to me (first char is '-')
{
if ( argv[i][1] == '-' ) // looks like an argument has double - ( -- ) at beigning
{
tmpString = substring( argv[i], 2, strlen ( argv[i] ));
fprintf(stderr, "-- " );
} else {
tmpString = substring( argv[i], 1, strlen ( argv[i] ));
fprintf(stderr, "- " );
}
fprintf(stderr, "%s\n", tmpString );
} else {
//fprintf(stderr, "I got option without argument\n" );
}
}
return 0;
}
Test:
Kod:
[webnull@webnull-linux-laptop src]$ ./eps asd qwerty --qwerty --fastpc
-- (null)
-- (null)
Powinno zwracać mniej więcej:
Kod:
[webnull@webnull-linux-laptop src]$ ./eps asd qwerty --qwerty --fastpc
-- qwerty
-- fastpc
Nie zapisuje do zmiennej tmpString, chodzi o podobne linie tmpString = substring( argv[i], 2, strlen ( argv[i] )); tak jak by zwracały NULL ( 0 )
-- WebNuLL
![[Obrazek: 1300013385.gif]](http://img860.imageshack.us/img860/6997/1300013385.gif)
Ubuntu (Linux dla ludzi) | Blog komputerowy | Linux Mint
(Ten post był ostatnio modyfikowany: 28.06.2010 23:44 przez WebNuLL.)
|
|
| 28.06.2010 23:40 |
|
WebNuLL
Linux support
Liczba postów: 1766
Dołączył: Sep 2009
Reputacja: 45
€: 4127
OS: Linux Gentoo
|
RE: Zasięg zmiennych czy źle napisana funkcja?
(29.06.2010 19:46)Propagandi napisał(a): Prawdopodobnie zwraca null, bo wykłada się na warunku: strlen(str) < (begin+len) w funkcji substring. Sama funkcja jest ok, tylko gdy jej używasz ostatni parametr musisz pomniejszyć o tyle znaków ile obciąłeś z początku ciągu, np: tmpString = substring( argv[i], 2, strlen(argv[i])-2);
Faktycznie, całkowicie o tym zapomniałem, dzięki sprawdzę to i dam znać.
@edit
Działa - dzięki, będę musiał poczytać dokumentację na temat strndup.
-- WebNuLL
![[Obrazek: 1300013385.gif]](http://img860.imageshack.us/img860/6997/1300013385.gif)
Ubuntu (Linux dla ludzi) | Blog komputerowy | Linux Mint
(Ten post był ostatnio modyfikowany: 29.06.2010 23:31 przez WebNuLL.)
|
|
| 29.06.2010 23:20 |
|
Użytkownicy przeglądający ten wątek: 1 gości