Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*questão 1*/
- #include <stdio.h>
- #include <stdlib.h>
- int main(){
- char c = 'a';
- char* pc = &c;
- printf("o endereço de ''c'' em hex: %p\no valor guardado em c é: %c\n\n", pc, c);
- printf("O valor que pc armazena e o valor guardado no endereço apontado por ele é: %p e ''%c''\n\n", pc, *pc);
- printf("o endereço de pc é: %p\n\n", &pc);
- printf("O endereço do valor guardado no endereço apontado por pc: %p \ne o valor guardado no endereço de pc: %p\n\n\n", &*pc, *&pc);
- /*eles são iguais por o *pc é a desreferencia do ponteiro pc, assim a gente vê para o q ele de fato ta apontando
- o &pc nos dá o endereço da variável pc
- pc -> c (apontando literalmente kkkk)
- &*pc -> &c
- &*pc = o endereço da variável q pc está apontando
- no caso do *&pc temos algo mais ou menos assim
- &pc = endereço q o valor de pc está guardado
- *&pc = o valor apontado pelo endereco de pc = c
- pois pois &pc obtém o endereço de pc e * obtém o valor armazenado nesse endereço, que é c
- isso é uma capoeira mental
- */
- return 0;
- }
- /*questão 2*/
- #include <stdio.h>
- int main(){
- int vet[5] = {1,2,3,4,5};
- int* p = vet;
- printf("endereço (inicial de vet) e endereço guardado em p: %p, %p\n\n\n", &vet, p);
- printf("os valores de vet são: ");
- for(int i = 0; i < 5 ; i ++){
- printf(" %d", *(p+i));
- }
- printf("\ne \n");
- for(int i = 0; i < 5 ; i ++){
- printf(" %d", p[i]);
- }
- printf("\n\n");
- for(int i = 0; i < 5 ; i ++){
- printf(" %d", vet[i]);
- }
- printf("\n\n");
- for(int i = 0; i < 5 ; i ++){
- printf(" %d", *(vet+i));
- }
- printf("\n\n");
- return 0 ;
- }
- /*questão 3*/
- #include <stdio.h>
- #include <string.h>
- int main( ){
- char str[50]; // String
- char str_inv[50]; // String invertida
- char *ptr_str = str;
- char *ptr_inv = str_inv;
- int i = -1;
- scanf(" %49s", str); // sem espaço
- // scanf(" %[^\n]", str);//com espacço
- //printf("\n%d\n\n", strlen(str));
- for (i; i < (int)strlen(str)-1; i++) {
- *(ptr_inv + strlen(str) - i - 2) = *(ptr_str + i+1); //fazer isso sem mudar nenhum valor foi foda, mas é só se ligar aonde começa e termina tudo
- }
- // Null-terminate the inverted string
- *(ptr_inv + strlen(str)) = '\0';
- printf("O inverso da string: %s\n\n", str_inv);
- return 0;
- }
- /*questão 3 com função*/
- #include <stdio.h>
- #include <string.h>
- void inverter(char* ptr_inv, char* ptr_str, char* str, char* str_inv){
- int i = - 1;
- for (i; i < (int)strlen(str)-1; i++) {
- *(ptr_inv + strlen(str) - i - 2) = *(ptr_str + i+1); //fazer isso sem mudar nenhum valor foi foda, mas é só se ligar aonde começa e termina tudo
- }
- *(ptr_inv + strlen(str)) = '\0';
- }
- int main( ){
- char str[50]; // String
- char str_inv[50]; // String invertida
- char *ptr_str = str;
- char *ptr_inv = str_inv;
- int i = -1;
- scanf(" %49s", str); // sem espaço
- // scanf(" %[^\n]", str);//com espacço
- inverter(ptr_inv, ptr_str, str, str_inv);
- *(ptr_inv + strlen(str)) = '\0';
- printf("O inverso da string: %s\n\n", str_inv);
- return 0;
- }
- /*questão 3 com recursividade*/
- #include <stdio.h>
- #include <string.h>
- void inverter(char* ptr_inv, char* ptr_str, int i, int len) {
- if (i >= len)
- return;
- *(ptr_inv + len - i - 1) = *(ptr_str + i + 1);
- inverter(ptr_inv, ptr_str, i + 1, len);
- }
- int main() {
- char str[50]; // String
- char str_inv[50]; // String invertida
- char *ptr_str = str;
- char *ptr_inv = str_inv;
- int i = -1;
- scanf(" %49s", str); // sem espaço
- inverter(ptr_inv, ptr_str, i, strlen(str) - 1);
- *(ptr_inv + strlen(str)) = '\0';
- printf("O inverso da string: %s\n\n", str_inv);
- return 0;
- }
- /*questão 4*/
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void ler(char*** str, int* indice) {
- //ponteiro triplo q aponta para a matriz
- int flag = 1;
- while (flag) {
- /*daqui até o fim da função a gente manipluna da mesma forma, a diferença é q são os ponteiros apontando para os indices da matriz*/
- (*str)[*indice] = (char*)malloc(1000 * sizeof(char));
- if ((*str)[*indice] == NULL) {
- printf("deu errado essa porra ae");
- flag = 0;
- }
- printf("digite a string: \n");
- if (fgets((*str)[*indice], 1000, stdin) != NULL) {
- if ((*str)[*indice][0] == '\n') {
- flag = 0;
- }
- else {
- (*str)[*indice][strcspn((*str)[*indice], "\n")] = '\0';
- (*indice)++;
- *str = realloc(*str, (*indice + 1) * sizeof(char*));
- if (*str == NULL) {
- printf("\n deu ruim isso aí ein\n");
- flag = 0;
- }
- }
- }
- else {
- flag = 0;
- }
- }
- }
- void mostrar(char*** str, int* indice){
- printf("\nas strings\n");
- for(int i = 0; i < *indice ; i++){
- printf("%s\n", (*str)[i]);
- }
- }
- void apagar(char*** str, int* indice){
- for(int i = 0; i < *indice ; i++){
- printf("apagando a strins '%s'\n", (*str)[i]);
- free((*str)[i]);
- }
- printf("apagando o resquicio da matriz\n");
- free((*str));
- }
- int main(){
- char** str = NULL;
- str = (char**)malloc(1 * sizeof(char*)); //malocando a quantidade de strings
- if(str == NULL){
- printf("\ndeu mto ruim saporra\n");
- }
- int indice = 0;
- ler(&str, &indice);
- mostrar(&str, &indice);
- apagar(&str, &indice);
- printf("\n\naté mais...\n\n");
- return 0;
- }
- /*questão 4 com função para basicamente tudo*/
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void ler(char*** str, int* indice) {
- //ponteiro triplo q aponta para a matriz
- int flag = 1;
- while (flag) {
- /*daqui até o fim da função a gente manipluna da mesma forma, a diferença é q são os ponteiros apontando para os indices da matriz*/
- (*str)[*indice] = (char*)malloc(1000 * sizeof(char));
- if ((*str)[*indice] == NULL) {
- printf("deu errado essa porra ae");
- flag = 0;
- }
- printf("digite a string: \n");
- if (fgets((*str)[*indice], 1000, stdin) != NULL) {
- if ((*str)[*indice][0] == '\n') {
- flag = 0;
- }
- else {
- (*str)[*indice][strcspn((*str)[*indice], "\n")] = '\0';
- (*indice)++;
- *str = realloc(*str, (*indice + 1) * sizeof(char*));
- if (*str == NULL) {
- printf("\n deu ruim isso aí ein\n");
- flag = 0;
- }
- }
- }
- else {
- flag = 0;
- }
- }
- }
- void mostrar(char*** str, int* indice){
- printf("\nas strings\n");
- for(int i = 0; i < *indice ; i++){
- printf("%s\n", (*str)[i]);
- }
- }
- void apagar(char*** str, int* indice){
- for(int i = 0; i < *indice ; i++){
- printf("apagando a strins '%s'\n", (*str)[i]);
- free((*str)[i]);
- }
- printf("apagando o resquicio da matriz\n");
- free((*str));
- }
- int main(){
- char** str = NULL;
- str = (char**)malloc(1 * sizeof(char*)); //malocando a quantidade de strings
- if(str == NULL){
- printf("\ndeu mto ruim saporra\n");
- }
- int indice = 0;
- ler(&str, &indice);
- mostrar(&str, &indice);
- apagar(&str, &indice);
- printf("\n\naté mais...\n\n");
- return 0;
- }
- /*questão 5*/
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void ler(char*** str, int* indice) {
- //ponteiro triplo q aponta para a matriz
- int flag = 1;
- while (flag) {
- /*daqui até o fim da função a gente manipluna da mesma forma, a diferença é q são os ponteiros apontando para os indices da matriz*/
- (*str)[*indice] = (char*)malloc(1000 * sizeof(char));
- if ((*str)[*indice] == NULL) {
- printf("deu errado essa porra ae");
- flag = 0;
- }
- printf("digite a string: \n");
- if (fgets((*str)[*indice], 1000, stdin) != NULL) {
- if ((*str)[*indice][0] == '\n') {
- flag = 0;
- }
- else {
- (*str)[*indice][strcspn((*str)[*indice], "\n")] = '\0';
- (*indice)++;
- *str = realloc(*str, (*indice + 1) * sizeof(char*));
- if (*str == NULL) {
- printf("\n deu ruim isso aí ein\n");
- flag = 0;
- }
- }
- }
- else {
- flag = 0;
- }
- }
- }
- void mostrar(char*** str, int* indice){
- printf("\nas strings\n");
- for(int i = 0; i < *indice ; i++){
- printf("%s\n", (*str)[i]);
- }
- }
- void apagar(char*** str, int* indice){
- for(int i = 0; i < *indice ; i++){
- printf("apagando a strins '%s'\n", (*str)[i]);
- free((*str)[i]);
- }
- printf("apagando o resquicio da matriz\n");
- free((*str));
- }
- int main(){
- char** str = NULL;
- str = (char**)malloc(1 * sizeof(char*)); //malocando a quantidade de strings
- if(str == NULL){
- printf("\ndeu mto ruim saporra\n");
- }
- int indice = 0;
- ler(&str, &indice);
- mostrar(&str, &indice);
- apagar(&str, &indice);
- printf("\n\naté mais...\n\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment