3

I got help with this question, and one of the answers suggested this technique, but i keep getting a Segmentation Fault (core dumped) error in this the code.

char *nam = array;//<-----(array is a string, we can use "test string" for it
int i = 0;
int length = strlen(array);
int count = 1;
printf("%i [%s]\n", length,nam);    
for(i; i < length; i++)
{
    puts(nam + i);
    nam[strlen(nam) - 1] = '\0';
}

What's supposed to happen, is I'm supposed to make a pyramid with the string, by removing one letter from the front, and one letter from the back. With this code, I managed to get rid of one letter from the front, but the error occurs on the "replacing" the last letter with a '\0'. Does anyone know why this is happening?

EDIT------------(updated code for clarification)

void pyramid(char array[])
{    
char nam[] = array;
int i = 0;
int length = strlen(nam);
int count = 1;
printf("%i [%s]\n", length, nam);   
for(i; i < length; i++)
{
    puts(nam + i);
    nam[strlen(nam) - 1] = '\0';
    printf("[%s]\n", nam);
}
}

main class

int main (void)
{
    char *name = "TEST STRING";
    pyramid(name);
}

hope this clarifies this code gives me an invalid initializer on the char[] = array; line

3 Answers 3

4
char *nam = array;//<-----(array is a string, we can use "test string" for it

you cann't modify the variable name, because of "test string" is allocated in character constant region, so you cann't modify it.

please use char nam[] = "test string"., in this case "test string" is allocated in stack, so you can modify it.

Sign up to request clarification or add additional context in comments.

1 Comment

oh i see, well it works now, but what i did was i called a method in the main class with the actual string, and this method is supposed to obtain the name, so how do i go about doing that? im going to re-edit my original so you see what im talking about
3

array is a string literal, you should not try to change it.

Change

char *nam = array;

to

char nam[] = array;

and try again.

By the way, the actual code should be look like

char name[] = "test string";

UPDATE:

Version 1, need C99 VLA

void pyramid(char array[])
{    
    int length = strlen(array);
    char nam[length+1];
    int i = 0;
    int count = 1;

    strcpy(nam, array);
    printf("%i [%s]\n", length, nam);   
    for(i; i < length; i++)
    {
        puts(nam + i);
        nam[strlen(nam) - 1] = '\0';
        printf("[%s]\n", nam);
    }
}

Version 2, use dynamical memory allocation:

void pyramid(char array[])
{    
    char *nam;
    int i = 0;
    int length = strlen(array);
    int count = 1;

    if ((nam = malloc(length+1)) == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }
    strcpy(nam, array);

    printf("%i [%s]\n", length, nam);   
    for(i; i < length; i++)
    {
        puts(nam + i);
        nam[strlen(nam) - 1] = '\0';
        printf("[%s]\n", nam);
    }

    free(nam);
}

2 Comments

@StevenR The actual code should be look like char name[] = "teststring";.
i actually updated my code on top for more clarification of what im trying to do.
1

A string literal is stored in a read-only memory segment. You should consider it a const char *, or immutable string.

If you are writing a function to perform a destructive operation on a string, it is good practice to make a copy of it anyway.

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

void pyramid_demo(const char *input) {
    /* Copy input to buf */
    int len = strlen(input);
    char *buf = malloc(len + 1);
    strcpy(buf, input);

    puts(buf);
    buf[len - 1] = '\0';    /* Now you can write to buf */
    puts(buf + 1);
}

int main(void) {
    /* The literal string is immutable */
    pyramid_demo("Hello world");
}

Critique of your pyramid() function

Aside from the segfault, there are other problems with your pyramid() function:

  • count is an unused variable.

  • In for(i, the i is useless. If you are using C99 or newer, you should delete the existing declaration/initialization of i, and write instead:

    for (int i = 0; i < length; i++)
    
  • char nam[] = array; is pointless aliasing. It doesn't make a copy of the input.

  • Avoid calling strlen() repeatedly, since each call requires walking down the entire string to look for the \0 terminator. You already set length at the beginning, so just keep track of the string length by decrementing length.

3 Comments

noobie mistakes, i know :( i just wish my teacher actually TAUGHT us this instead he just talks about real world stuff, and doesnt teach, thank you for your help
Please indicate you appreciation by voting up useful answers instead of commenting. Your votes are what make Stack Overflow work.
oh woops! forgot to do that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.