Segmentation fault (core dumped) when accessing String

Asked

Viewed 912 times

0

I built the code below to separate only the last characters of a link (the last 11 to be exact). Until then everything works well, calculations are done normally, values also match. The problem is the time to give printf in the String(vector) values, the problem is returned Segmentation fault (core dumped).

#include <stdio.h>
#include <string.h>

int main(){

  char link[100];
  int linkSize, calcLink;

  scanf("%s", &link);
  //lê o tamanho da string pra capturar os últimos caracteres
  linkSize = strlen(link);

  printf("\nTamanho da String: %i\n", linkSize);

  calcLink = linkSize - 11;

  printf("%s\n", link);

  int i = calcLink;
  while(i < linkSize){
    printf("%s\n", link[i]);
    ++i;
  }
  return 0;
}

How to access the last memory addresses of my String link and how to store these last characters in another string. I hope I was clear. I know it’s pretty basic, but as much as I looked, I couldn’t find an answer that could solve this particular case. I thank you for your understanding.

1 answer

3


There are some things you need to get right:

  • When does printf with %s is supposed to pass a pointer as parameter, which was something that was missing in:

    printf("%s\n", link[i]);
    

    Since you are passing a character the most suitable formatter would be %c.

  • In scanf("%s", &link); has a & the more the scanf must receive the memory address where it will put the read and link for being a pointer already refers to a memory address.

  • Failed to include <string.h> which is where the function comes from strlen

But if you just want to show the result you can simply do printf %s passing the base address that is link and adding up X Memory positions until reaching the last 11, there is no need for a loop:

linkSize = strlen(link);
printf("\nTamanho da String: %i\n", linkSize);

calcLink = linkSize - 11;
if (calcLink < 0) calcLink = 0; //nao deixar a posição ser negativa

//aqui soma ao ponteiro link as posições de memoria necessárias para chegar aos ultimos 11
printf("%s\n", link + calcLink);

Example of this code working on Ideone

If you want to build a new string with this text simply allocate statically or dynamically and copy the characters both manually and with memcpy

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

void last_link(char* link, int char_count, char* last_chars){
    int linkSize = strlen(link);
    int calcLink = linkSize - char_count;
    if (calcLink < 0) calcLink = 0;

    memcpy(last_chars, link + calcLink, char_count); //copiar os carateres
    last_chars[char_count] = '\0'; //colocar o terminador
}

int main() {
    char link[100];

    scanf("%s", link);
    printf("\nTamanho da String: %i\n", strlen(link));

    char last_chars[12]; //tem de ter mais 1 caractere para o terminador \0
    last_link(link, 11, last_chars); //obter os últimos 11

    printf("%s\n", last_chars);

    return 0;
}

Example of this code also in Ideone

I could have created the new string directly within the function last_link with malloc but that would imply that whoever called it the function of not being able to forget to do free otherwise would have a memory leak.

  • Interesting. Every time I explore this forum I am more surprised by how much knowledge we can acquire here. It’s nice to see experienced people are willing to help neophytes like me. Thank you for your answer, it was very helpful, I never really understood the operation of pointers, in fact I never really cared much. I see that this directly affects the development of a program. I thank you for your help!

  • @I’m glad I could help. When programming in C, unless they are very simple things it is common to have to deal with pointers, and so it is important to know how to work with them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.