使用char指针->分段错误

时间:2019-02-14 20:37:00

标签: c pointers char segmentation-fault fault

我是一个初学者,在学校里,我们有任务要使用指针分割char数组。我想分割名称和生日(定界符='。')。我的解决方案非常适合该名称,但是在生日那天这样做却遇到了细分错误。

int main(int argc, char *argv[])
{
    char *fullname;
    char *name;
    char *lastname;
    char *birthday;
    char *day;
    fullname = argv[1];
    birthday = argv[2];
    int i = 0;
    int y = 0;
    int z = 0;

    while(fullname[i] != '.'){
        char x = fullname[i];
        name[i] = x;
        fullname[i] = ' ';
        i++;
    }
    i++;

    while(fullname[i] != '\0'){
        char x = fullname[i];
        lastname[y] = x;
        fullname[i] = ' ';
        i++;
        y++;
     }

    while(birthday[z] != '.'){
         char b = birthday[z];  
         day[z] = b;
         z++;
    }
}

在命令行中输入(./a是cygwin上的exe文件):

./a mister.gold 11.05.2005

输出:

segmentation fault

当我启动没有最后一个循环的代码时,我的输出是:

mister
gold

1 个答案:

答案 0 :(得分:0)

sfxDeath

两个char数组指针,但是它们指向两个什么呢?没什么,所以您基本上是在尝试访问您不拥有的内存 为他们分配内存,您应该这样做:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;

public class Player : MonoBehaviour
{
    [SerializeField] private float jumpForce = 100f;
    [SerializeField] private float forwardMomentum = 5f;
    [SerializeField] private AudioClip sfxJump;
    [SerializeField] private AudioClip sfxDeath;
    [SerializeField] private AudioClip sfxCoin; 

    private Animator anim;
    private Rigidbody Rigidbody;
    private bool jump = false;
    private AudioSource audioSource;  

    private void Awake()
    {
        Assert.IsNotNull(sfxJump);
        Assert.IsNotNull(sfxDeath);
        Assert.IsNotNull(sfxCoin);
    }

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        Rigidbody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();        
    }

    // Update is called once per frame
    void Update()
    {
        if (!GameManager.instance.GameOver() && GameManager.instance.GameStarted())
        { 
            if (Input.GetMouseButton(0))
            {
                GameManager.instance.PlayerStartedGame();

                anim.Play("Jump");
                audioSource.PlayOneShot(sfxJump);
                Rigidbody.useGravity = true;
                jump = true;
            }
        }
    }

    private void FixedUpdate()
    {
        if (jump)
        {
            jump = false;
            Rigidbody.velocity = new Vector2(0, 0);
            Rigidbody.AddForce(new Vector2(forwardMomentum, jumpForce), ForceMode.Impulse);
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        switch (collision.gameObject.tag)
        {
            case "obstacle":
                Rigidbody.AddForce(new Vector2(-50, 20), ForceMode.Impulse);
                Rigidbody.detectCollisions = false;
                audioSource.PlayOneShot(sfxDeath);
                GameManager.instance.PlayerCollided();
                break;
            case "coin":

                audioSource.PlayOneShot(sfxCoin);
                GameManager.instance.Score(1);
                print("GOT COIN");
                break;

        }
    }
}

现在它们指向您可以访问和修改的内容

使用完它们后,应该像这样

释放动态分配的内存
char *name ;
char *lastname;