Localhost Posted December 7, 2009 at 09:20 PM Report Share #299134 Posted December 7, 2009 at 09:20 PM Olá pessoal, bem o Linux está-me a dar alguns problemas em termos de programação e confesso que tinha muitas mais facilidades no Windows... Ora bem é o seguinte eu estou aqui com um projecto que está a dar mesmo pica 😉, no entanto o Linux não me deixa ver os resultados, eu compilo tudo direitinho mas quando chega a hora de executar aparece o seguinte: Segmentation Fault, alguém me podia ajudar? Obrigado desde já, a todos! here since 2009 Link to comment Share on other sites More sharing options...
msr Posted December 7, 2009 at 09:26 PM Report Share #299135 Posted December 7, 2009 at 09:26 PM Segmetation fault é sinónimo de que acedeste a memória indevidamente. Sem ver código não dá para ajudar muito mais. Vê bem o código que tens feito. COMPONENT ORGANIZER easily organize your electronic parts and application notes Link to comment Share on other sites More sharing options...
Localhost Posted December 7, 2009 at 09:33 PM Author Report Share #299137 Posted December 7, 2009 at 09:33 PM Desculpa esqueci-me do código ? Aí vai: #include <stdio.h> #include <string.h> int main() { int x,i,len=4; char str[4]; char abc[26] = {'a','b','c','d', 'e','f','g','h','i','j','l','m','n','o','p','q','r','s', 't','u','v','x','z','k','y','\0'}; while(strlen(str) <= len) { srand(time(NULL)); x = rand() % 26 + 1; str[i] = abc[x]; i++; } printf("%s", str); return 0; } here since 2009 Link to comment Share on other sites More sharing options...
Ferreira Posted December 8, 2009 at 12:05 AM Report Share #299169 Posted December 8, 2009 at 12:05 AM Não inicializaste o i. http://twitter.com/ferreira Link to comment Share on other sites More sharing options...
Localhost Posted December 8, 2009 at 12:26 AM Author Report Share #299175 Posted December 8, 2009 at 12:26 AM LOL, claro, que erro estupido, obrigado 😉 here since 2009 Link to comment Share on other sites More sharing options...
Localhost Posted December 8, 2009 at 02:16 PM Author Report Share #299242 Posted December 8, 2009 at 02:16 PM Olá pessoal, ainda um problema com o mesmo código... Eu pus esse while de maneira a que cada vez que entrasse no laço mudasse o valor de x coisa que não está a acontecer e eu não percebo porquê, era suposto ele mudar o valor certo? here since 2009 Link to comment Share on other sites More sharing options...
Localhost Posted December 8, 2009 at 02:23 PM Author Report Share #299245 Posted December 8, 2009 at 02:23 PM Eheheh, já percebi o problema do meu post anterior só que agora tenho outro... No final de cada string aparece sempre um simbolo estranho ehhehe, como retiro isto? Já vi se era do nulo mas também não muda... here since 2009 Link to comment Share on other sites More sharing options...
Localhost Posted December 8, 2009 at 02:26 PM Author Report Share #299247 Posted December 8, 2009 at 02:26 PM Pronto, já está LOL, já resolvi todos os problemas, obrigado a todos 😉 here since 2009 Link to comment Share on other sites More sharing options...
Baderous Posted December 8, 2009 at 02:39 PM Report Share #299251 Posted December 8, 2009 at 02:39 PM Não sei porquê, mas devido ao facto de não teres o str inicializado, o strlen estava a devolver-me o valor 7, pelo que não processava o ciclo. Isso do símbolo é porque estás a escrever mais caracteres para o array do que aqueles que ele suporta. EDIT: Já sei o porquê do strlen dar 7. É que este utiliza o '\0' como marca do fim de contagem do número de caracteres, e devido ao facto de não teres inicializado o str, o strlen vai dar um valor diferente que nunca se saberá qual é, depende sempre da existência de um 0 na memória. The length of a C string is determined by the terminating null-character: A C string is as long as the amount of characters between the beginning of the string and the terminating null character. Link to comment Share on other sites More sharing options...
Localhost Posted December 8, 2009 at 03:34 PM Author Report Share #299272 Posted December 8, 2009 at 03:34 PM Mas o C não põe o nulo no final automaticamente sem ter de inicializar a string? here since 2009 Link to comment Share on other sites More sharing options...
Ferreira Posted December 8, 2009 at 03:35 PM Report Share #299274 Posted December 8, 2009 at 03:35 PM Não, aquilo para todos os efeitos é um char *. Existem é funções que os tratam como strings. http://twitter.com/ferreira Link to comment Share on other sites More sharing options...
Localhost Posted December 8, 2009 at 05:07 PM Author Report Share #299288 Posted December 8, 2009 at 05:07 PM Ui, eu sempre pensei que ele punha sozinho o terminador nulo LOL, pelo menos nunca tive problemas deste género :dontgetit: here since 2009 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now